如何限制具有相同外键的实体

时间:2015-10-29 14:50:34

标签: mysql sql

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)

我想通过bCOUNT(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

的实体

0 个答案:

没有答案