搜索左连接null

时间:2011-08-09 23:56:53

标签: mysql sql

我有一个mysql的sql语句,它将字符串与两个数据库中的单词匹配。我想排除同样在另一个表中的结果,但是当我添加conv_progress的左连接时,它会破坏该语句。任何人都有任何想法如何排除另一个表中的条目?

作品

SELECT video.*,
  MATCH(video.title) AGAINST(? IN BOOLEAN MODE) as cscore, 
  MATCH(tags.name) AGAINST(? IN BOOLEAN MODE) as htscore
FROM video
LEFT JOIN tags ON video.vid_id=tags.vid_id
null
WHERE
  MATCH(video.title) AGAINST(? IN BOOLEAN MODE) OR
  MATCH(tags.name) AGAINST(? IN BOOLEAN MODE) GROUP BY video.vid_id
ORDER BY cscore DESC LIMIT ?, ?

SELECT video.*,
  MATCH(video.title) AGAINST(? IN BOOLEAN MODE) as cscore, 
  MATCH(tags.name) AGAINST(? IN BOOLEAN MODE) as htscore
FROM video
LEFT JOIN tags ON video.vid_id=tags.vid_id
LEFT JOIN conv_progress on video.vid_id=conv_progress.vid_id where conv_progress.vid_id is null
WHERE
  MATCH(video.title) AGAINST(? IN BOOLEAN MODE) OR
  MATCH(tags.name) AGAINST(? IN BOOLEAN MODE) GROUP BY video.vid_id
ORDER BY cscore DESC LIMIT ?, ?

2 个答案:

答案 0 :(得分:1)

conv_progress.vid_id is null移到WHERE子句中:

SELECT video.*,
  MATCH(video.title) AGAINST(? IN BOOLEAN MODE) as cscore, 
  MATCH(tags.name) AGAINST(? IN BOOLEAN MODE) as htscore
FROM video
LEFT JOIN tags ON video.vid_id=tags.vid_id
LEFT JOIN conv_progress on video.vid_id=conv_progress.vid_id 
WHERE
  (
    MATCH(video.title) AGAINST(? IN BOOLEAN MODE) OR
    MATCH(tags.name) AGAINST(? IN BOOLEAN MODE)
  ) 
  AND conv_progress.vid_id is null
GROUP BY video.vid_id
ORDER BY cscore DESC LIMIT ?, ?

答案 1 :(得分:1)

您也可以使用NOT EXISTS代替加入

SELECT video.*,
  MATCH(video.title) AGAINST(? IN BOOLEAN MODE) as cscore, 
  MATCH(tags.name) AGAINST(? IN BOOLEAN MODE) as htscore
FROM video
LEFT JOIN tags ON video.vid_id=tags.vid_id
null
WHERE
  (
  MATCH(video.title) AGAINST(? IN BOOLEAN MODE) OR
  MATCH(tags.name) AGAINST(? IN BOOLEAN MODE)
  )AND(NOT EXISTS(SELECT 1 FROM conv_progress WHERE video.vid_id=conv_progress.vid_id))
 GROUP BY video.vid_id
ORDER BY cscore DESC LIMIT ?, ?