【发布时间】:2014-02-03 14:10:09
【问题描述】:
我有 3 张桌子 song、author、song_author。
连接是song 1--* song_author 1--* author
我使用类似的查询
SELECT *
FROM song
LEFT JOIN song_author ON s_id = sa_song
LEFT JOIN author ON sa_author = a_id
对于将导致 3 行的给定示例:
John - How beautiful it is
John - Awesome
George - Awesome
我正在这样填充我的对象,所以很好。
但是,我想添加一个 LIMIT 子句,但因为它只为一首歌曲返回几行 LIMIT 10 并不总是显示 10 首歌曲。
我知道的另一种可能性是打印所有歌曲,然后在里面进行第二次查询,但这会导致O(n),我想避免这种情况。
author:
+------+--------+
| a_id | a_name |
+------+--------+
| 1 | John |
| 2 | George |
+------+--------+
song:
+------+---------------------+
| s_id | s_name |
+------+---------------------+
| 1 | How beautiful it is |
| 2 | Awesome |
+------+---------------------+
song_author:
+-----------+---------+
| sa_author | sa_song |
+-----------+---------+
| 1 | 1 |
| 1 | 2 |
| 2 | 2 |
+-----------+---------+
【问题讨论】:
-
我认为你的联想是错误的。而不是
song *--1 song_author *--1 author,你真的有song 1--* song_author *--1 author,对吧?一首歌可以有很多作者(通过 song_author),一个作者可以有很多歌曲(通过 song_author)。 -
@omgitsfletch 是的,我会解决的,你
标签: mysql sql normalization database-normalization