这是一个有趣的例子。更好的格式:
SELECT SQL_CALC_FOUND_ROWS *, t.name as tname
FROM video, type as t INNER JOIN
user
ON video.seller = user.id
WHERE video.type2 = t.id AND
thumbnail_blobKey != '' AND disabled = 0 AND is_reel = 0 AND price!= 0 AND video.type != 4
GROUP BY video.id
ORDER BY video.id DESC
LIMIT 0, 10
问题video
是from
由于,
. 这就是逗号的工作原理。有趣的是,它可以通过,
用它的逻辑等价替换来修复cross join
:
SELECT SQL_CALC_FOUND_ROWS *, t.name as tname
FROM video cross join type as t INNER JOIN
user
ON video.seller = user.id
WHERE video.type2 = t.id AND
thumbnail_blobKey != '' AND disabled = 0 AND is_reel = 0 AND price!= 0 AND video.type != 4
GROUP BY video.id
ORDER BY video.id DESC
LIMIT 0, 10
这在文档中进行了解释,原因是优先级。逗号的优先级低于cross join
。因此,表达式中的连接被评估为“视频连接(类型连接用户)”。因为第二个连接首先被解释,所以其中的列video
是未知的。
但是,表上确实有一个join,所以最好写成:
SELECT SQL_CALC_FOUND_ROWS *, t.name as tname
FROM video join
type t
on video.type2 = t.id INNER JOIN
user
ON video.seller = user.id
WHERE thumbnail_blobKey != '' AND disabled = 0 AND is_reel = 0 AND price!= 0 AND video.type != 4
GROUP BY video.id
ORDER BY video.id DESC
LIMIT 0, 10