CREATE TABLE a {
id PRIMARY KEY
};
CREATE TABLE b {
id PRIMARY KEY,
a_id INT,
FOREIGN KEY (a_id) REFERENCES a(id)
}
CREATE TABLE c {
id PRIMARY KEY,
b_id INT,
FOREIGN KEY (b_id) REFERENCES b(id)
我想通过b
从COUNT(c.id)
订购前30个实体,例如
SELECT b.id, a_id FROM b
JOIN c ON b_id = b.id
GROUP BY b.id
ORDER BY COUNT(c.id)
LIMIT 30;
但请确保不会有超过6个具有相同a_id