说我有两张桌子想加入.分类:
id name ---------- 1 Cars 2 Games 3 Pencils
和项目:
id categoryid itemname --------------------------- 1 1 Ford 2 1 BMW 3 1 VW 4 2 Tetris 5 2 Pong 6 3 Foobar Pencil Factory
我想要一个返回类别和第一个(也是唯一的)itemname的查询:
category.id category.name item.id item.itemname ------------------------------------------------- 1 Cars 1 Ford 2 Games 4 Tetris 3 Pencils 6 Foobar Pencil Factory
有没有办法可以获得随机结果:
category.id category.name item.id item.itemname ------------------------------------------------- 1 Cars 3 VW 2 Games 5 Pong 3 Pencils 6 Foobar Pencil Factory
谢谢!
刚做了一个快速测试.这似乎有效:
mysql> select * from categories c, items i -> where i.categoryid = c.id -> group by c.id; +------+---------+------+------------+----------------+ | id | name | id | categoryid | name | +------+---------+------+------------+----------------+ | 1 | Cars | 1 | 1 | Ford | | 2 | Games | 4 | 2 | Tetris | | 3 | Pencils | 6 | 3 | Pencil Factory | +------+---------+------+------------+----------------+ 3 rows in set (0.00 sec)
我认为这将满足你的第一个问题.不确定第二个 - 我认为需要一个内部查询与random()或类似的东西!