sql查询帮助(非常具体)

时间:2010-01-14 00:46:50

标签: sql

我已经浏览了整个互联网,以回答我的问题,为什么我的sql语句返回false。我在mimer上检查了sql验证器,我得到的是我使用了保留字名。我的数据库中应该有一些匹配的东西,所以这里是:

以下是我创建sql语句的方法:

$title = 'SELECT * FROM item, categories WHERE item.title 
LIKE "%'.implode('%" OR item.title LIKE "%', $data).'%"'.' 
AND categories.name = '.$category;

这就是结果:

SELECT * FROM item, categories WHERE item.title LIKE "%hello%" 
OR item.title LIKE "%world%" OR item.title 
LIKE "%Joomla%" OR item.title LIKE "%Animal%" AND categories.name = Book

3 个答案:

答案 0 :(得分:3)

你应该考虑你的(缺乏)括号。我不确定你要完成什么,

但是这个:

SELECT
    *
FROM item, categories
WHERE item.title LIKE "%hello%"
OR item.title LIKE "%world%"
OR item.title LIKE "%Joomla%"
OR item.title LIKE "%Animal%"
AND categories.name = "Book"

与此截然不同:

SELECT
    *
FROM item, categories
WHERE (
    item.title LIKE "%hello%"
    OR item.title LIKE "%world%"
    OR item.title LIKE "%Joomla%"
    OR item.title LIKE "%Animal%"
)
AND categories.name = "Book"

另外,和其他人一样,Book应该用引号(就像我在这里做的那样)。

AND categories.name = "Book"

答案 1 :(得分:0)

您需要用引号括住Book这个词。

我打赌你也需要括号。

SELECT * FROM item, categories WHERE (item.title LIKE "%hello%" 
OR item.title LIKE "%world%" OR item.title 
LIKE "%Joomla%" OR item.title LIKE "%Animal%") AND categories.name = "Book"

答案 2 :(得分:0)

最后一部分,categories.name,是一个字符串?如果是的话应该是

and categories.name = 'book'

注意书周围的引语