[英]Mysql left join with limit returning join record for one row
How do i join a table using limit?如何使用限制加入表? I have the below query but it doesn't work as expected.
我有以下查询,但它没有按预期工作。 Am using
left join
to select only one row from table, but it only select one record as expected for the first row while it returns null on others Even when they have file saved in TABLE_USER_FILES
.我正在使用
left join
联接到 select 表中只有一行,但它只有 select 一条记录,如预期的第一行,而它返回 null 其他人即使他们保存在TABLE_USER_FILES
文件中
TABLE_USERS TABLE_USERS
uid | u_name
----|---------
p1 | Peter
j1 | John
f1 | Foo
b1 | Bar
TABLE_USER_POST TABLE_USER_POST
pid | p_name | p_uid
----|---------|--------
xp1 | PHP | p1
xp2 | SQL | p1
xp3 | JS | j1
xp4 | CSS | b1
TABLE_USER_FILES TABLE_USER_FILES
fid | f_uid | f_url | f_path
----|--------|---------|----------
fa1 | p1 | ax.png | gallery
fb2 | p1 | bc.png | gallery
bc3 | j1 | cc.png | gallery
fd4 | f1 | cx.png | gallery
fe5 | j1 | qd.png | gallery
Query询问
SELECT post.*, user.u_name, files.f_url
FROM TABLE_USER_POST post
INNER JOIN TABLE_USERS user
ON user.uid = post.p_uid
LEFT JOIN (
SELECT f_url, f_uid
FROM TABLE_USER_FILES
WHERE f_path = "gallery"
ORDER BY fid DESC
LIMIT 1
) files
ON files.f_uid = post.p_uid
ORDER BY post.pid DESC
LIMIT 0, 20
Expected result预期结果
pid | p_name | p_uid | u_name | f_url
----|---------|--------|---------|---------
xp1 | PHP | p1 | Peter | bc.png
xp2 | SQL | p1 | Peter | bc.png
xp3 | JS | j1 | John | qd.png
xp4 | CSS | b1 | Bar | NULL
Please try this instead.请改用这个。
SELECT post.*, user.u_name, files.f_url FROM TABLE_USER_POSTS post LEFT JOIN TABLE_USER_FILES files ON files.f_uid = post.p_uid AND files.fid = (SELECT MAX(fid) FROM TABLE_USER_FILES WHERE f_uid = files.f_uid) INNER JOIN TABLE_USERS user ON user.uid = post.p_uid ORDER BY post.pid DESC;
Thank you!谢谢!
There are many solutions here.这里有很多解决方案。 For example, LATERAL in MySQL 8.0.14+
例如,MySQL 8.0.14+ 中的LATERAL
SELECT post.*, user.u_name, files.f_url
FROM TABLE_USER_POST post
INNER JOIN TABLE_USERS user
ON user.uid = post.p_uid
LEFT OUTER JOIN LATERAL (
SELECT f_url, f_uid
FROM TABLE_USER_FILES tuf
WHERE f_path = "gallery"
AND tuf.f_uid = post.p_uid
ORDER BY fid DESC
LIMIT 1
) files ON true
ORDER BY post.pid DESC
LIMIT 0, 20
If only one column from TABLE_USER_FILES is needed, then the query in the SELECT clause:如果只需要 TABLE_USER_FILES 中的一列,则 SELECT 子句中的查询:
SELECT post.*, user.u_name,
( SELECT f_url
FROM TABLE_USER_FILES tuf
WHERE f_path = "gallery"
AND tuf.f_uid = post.p_uid
ORDER BY fid DESC
LIMIT 1
) AS f_url
FROM TABLE_USER_POST post
INNER JOIN TABLE_USERS user
ON user.uid = post.p_uid
ORDER BY post.pid DESC
LIMIT 0, 20
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.