4

第一个查询有效:

SELECT video.seller from video;

第二个不是:

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

错误:“on 条款”中的未知列“video.seller”

4

2 回答 2

3

这是一个有趣的例子。更好的格式:

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

问题videofrom由于,. 这就是逗号的工作原理。有趣的是,它可以通过,用它的逻辑等价替换来修复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
于 2013-06-20T00:23:57.927 回答
1

正如现在所写的那样,您正在尝试连接表type,并且user有条件video.seller = user.id。由于 video.seller 既不是表格的一列,type也不是user,您有一个错误。

更改为以下内容将消除此错误:

    SELECT SQL_CALC_FOUND_ROWS *,
       t.name AS tname
FROM video
INNER JOIN USER ON video.seller = USER.id,
                                       TYPE AS t
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
于 2013-06-20T00:23:44.970 回答