举例来说...
CREATE TABLE colours(colour_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,colour VARCHAR(20) NOT NULL);
INSERT INTO colours VALUES (1,'red'),(2,'orange'),(3,'yellow'),(4,'green'),(5,'blue'),(6,'indigo'),(7,'violet');
CREATE TABLE things
(thing VARCHAR(20) NOT NULL PRIMARY KEY,colour VARCHAR(20));
INSERT INTO things VALUES
('tomato','red'),
('cherry','red'),
('heart','red'),
('ferrari','red'),
('chrysanthemum','orange'),
('orange','orange'),
('banana','yellow'),
('lemon','yellow'),
('sunflower','yellow'),
('caterpillar','green'),
('cucumber','green'),
('grass','green'),
('sky','blue'),
('suede shoes','blue'),
('bluebell','blue'),
('indigo bunting','indigo'),
('violets','violet');
SELECT c.colour
, y.thing
FROM colours c
JOIN things x
ON x.colour = c.colour
JOIN things y
ON y.colour = x.colour
AND y.thing <= x.thing
WHERE c.colour_id <=3
GROUP
BY c.colour,x.thing
HAVING COUNT(*) <=3
ORDER
BY colour_id;
+--------+---------------+
| colour | thing |
+--------+---------------+
| red | cherry |
| red | cherry |
| red | cherry |
| orange | chrysanthemum |
| orange | chrysanthemum |
| yellow | banana |
| yellow | banana |
| yellow | banana |
+--------+---------------+
http://www.sqlfiddle.com/#!2/36937/1
哦,好的。我没有想过这样做。我故意简化了我的OP表结构。媒体表格连接到其他表格,并有更多字段,因此最终将媒体记录作为PHP中的关联数组而非逗号分隔会更有用。但是,这是一个很好的解决方案。 – SpongeBobPHPPants