sql搜索查询仍然没有产生预期的结果

时间:2010-09-09 14:38:09

标签: php sql mysql select

我需要计数和搜索查询才能正常工作。计数查询似乎正常工作,但搜索查询不是。

计数查询:

SELECT COUNT(DISTINCT tg_id) 
  FROM tg_keywords 
 WHERE tg_keyword LIKE 'keyword_1' OR tg_keyword LIKE 'keyword_2' 
 ORDER BY tg_keyword LIKE 'keyword_1' AND tg_keyword LIKE 'keyword_2 DESC
Returns 1 count as expected.

搜索查询:

SELECT DISTINCT tg_keywords.tg_id 
  FROM tg_keywords LEFT JOIN tg_info.tg_id=tg_keywords.tg_id 
 WHERE tg_keyword LIKE 'keyword_1' OR tg_keyword LIKE 'keyword_2' 
 ORDER BY tg_keyword LIKE 'keyword_1' AND tg_keyword LIKE 'keyword_2' DESC, tg_info.date_added LIMIT 16 OFFSET 1
Returns 0 results (1 is expected)

非常感谢任何建议

提前致谢, 阿奇

3 个答案:

答案 0 :(得分:2)

删除查询末尾的OFFSET 1,然后尝试。

OFFSET 1告诉它给你第二行的记录。将它OFFSET 0或完全删除它。

答案 1 :(得分:0)

SELECT DISTINCT tg_keywords.tg_id 
FROM tg_keywords 
LEFT JOIN tg_info.tg_id=tg_keywords.tg_id 
WHERE tg_keyword LIKE 'keyword_1' OR tg_keyword LIKE 'keyword_2' 
ORDER BY tg_keyword LIKE 'keyword_1' AND tg_keyword LIKE 'keyword_2' DESC, tg_info.date_added 
LIMIT 16 OFFSET 1

ORDER BY部分是什么?你的意思是:

WHERE tg_keyword LIKE 'keyword_1' OR tg_keyword LIKE 'keyword_2' 
ORDER BY tg_info.date_added DESC

答案 2 :(得分:0)

您是否也想让用户能够使用SQL通配符?如果不是,我可能会喜欢并输入=。