我有 3 个表 song
、author
、song_author
。
连接是歌曲 1--* Song_author 1--* 作者
我使用这样的查询
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 |
+-----------+---------+
最佳答案
你可以尝试这样的事情:
SELECT *
FROM
(SELECT *
FROM song
LIMIT 10) r
LEFT JOIN song_author ON r.s_id = sa_song
LEFT JOIN author ON sa_author = a_id
关于mysql - 规范化表 LIMIT 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21520227/