[英]MySQL: subquery in LEFT JOIN
I have a problem. 我有个问题。 I want to use subquery tm
in LEFT JOIN .. ON
我想在LEFT JOIN .. ON
使用子查询tm
LEFT JOIN .. ON
SELECT t.*,
(SELECT `uid` FROM `truck_transport` tm WHERE tm.from = t.station ORDER BY RAND() LIMIT 1) as tm
FROM `truck_trailer` t
LEFT JOIN `truck_transport` tm2 ON (tm2.uid = tm) ...
If I use subquery in FROM
result of rand is always the same. 如果我在FROM
使用子查询,则结果总是相同。
Sorry for my language :/ 对不起,我的语言:/
try this 尝试这个
SELECT t.*, tm.uid
FROM `truck_trailer` t
LEFT JOIN (SELECT `uid` FROM `truck_transport` ORDER BY RAND() LIMIT 1) as tm
ON (tm.uid = t.station)
mynawaz has written a correct query. mynawaz已编写了正确的查询。
Your subquery will always return only one result because of limit 1
. 由于limit 1
您的子查询将始终仅返回一个结果。 If you want only one result to come then use JOIN
instead of LEFT JOIN
. 如果只希望得到一个结果,则使用JOIN
而不是LEFT JOIN
。 because left join table always returns matching rows and non matching with NULL
of right side table. 因为左连接表总是返回匹配的行,而与右侧表的NULL
不匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.