此SQL连接查询出了什么问题

时间:2018-12-13 07:12:27

标签: sql join

因此,所有字段和表都存在并且具有相关数据(已选中)。 但这并没有给我任何结果。

SELECT `r`.*, `k`.`keyword`, `c`.`name`, `c`.`state`
FROM `local_rank` as `r`
LEFT JOIN `keyword` as `k` ON `r`.`keyword_id` = `k`.`keyword_id`
LEFT JOIN `campaign` as `c` ON `k`.`campaign_id` = `c`.`campaign_id`
WHERE `c`.`campaign_id` = '6'
AND `r`.`location` = '60602'
ORDER BY `local_rank_id` DESC
 LIMIT 6, 60602

当我删除任何一个WHERE / AND条件时,它都会获取数据行。

2 个答案:

答案 0 :(得分:1)

由于您使用带偏移量的“ LIMIT”,因此至少应显示60602条以上的记录。您可以通过删除LIMIT偏移量和 计算返回的行数。

SELECT `r`.*, `k`.`keyword`, `c`.`name`, `c`.`state`
FROM `local_rank` as `r`
LEFT JOIN `keyword` as `k` ON `r`.`keyword_id` = `k`.`keyword_id`
LEFT JOIN `campaign` as `c` ON `k`.`campaign_id` = `c`.`campaign_id`
WHERE `c`.`campaign_id` = '6'
AND `r`.`location` = '60602'
ORDER BY `local_rank_id` DESC
 LIMIT 6

答案 1 :(得分:1)

从限制中删除偏移量,您可能没有超过60602行与您的查询匹配

SELECT `r`.*, `k`.`keyword`, `c`.`name`, `c`.`state`
FROM `local_rank` as `r`
LEFT JOIN `keyword` as `k` ON `r`.`keyword_id` = `k`.`keyword_id`
LEFT JOIN `campaign` as `c` ON `k`.`campaign_id` = `c`.`campaign_id`
WHERE `c`.`campaign_id` = '6'
  AND `r`.`location` = '60602'
ORDER BY `local_rank_id` DESC
LIMIT 6