我想做一个包含“like”和“not like”的查询。当前查询:
SELECT *
FROM `final`
WHERE ( `T_Degree` LIKE '%Keyword1%' )
and (`A_Author_Translator_State` NOT LIKE 'Keyword2'
or `T_Author` NOT LIKE 'Keyword2'
or `T_Author_State` NOT LIKE 'Keyword2' )
ORDER BY `Kind` ASC但这并不管用。有什么建议吗?
发布于 2013-12-11 00:50:56
只需将or更改为and,假设您希望这三个字段中的任何一个字段都不像“Keyword2”:
SELECT *
FROM `final`
WHERE ( `T_Degree` LIKE '%Keyword1%' )
and (`A_Author_Translator_State` NOT LIKE 'Keyword2'
and `T_Author` NOT LIKE 'Keyword2'
and `T_Author_State` NOT LIKE 'Keyword2' )
ORDER BY `Kind` ASC;顺便说一句,因为您没有使用通配符,所以可以这样表述:
SELECT *
FROM final
WHERE T_Degree LIKE '%Keyword1%' and
Keyword2 not in (A_Author_Translator_State, T_Author, T_Author_State)
ORDER BY Kind ASC;发布于 2013-12-11 00:34:30
你可能需要更好地阐明你试图实现的是什么,但这可能会对你想要的东西起作用。
SELECT *
FROM `final`
WHERE ( `T_Degree` LIKE '%Keyword1%' )
AND NOT (`A_Author_Translator_State` LIKE 'Keyword2'
or `T_Author` LIKE 'Keyword2'
or `T_Author_State` LIKE 'Keyword2' )
ORDER BY `Kind` ASC发布于 2013-12-11 01:01:19
如果不使用通配符(%),基本上就是在检查两者是否相同。
换句话说:
WHERE `ColumnName` LIKE 'Value' 与以下内容相同:
WHERE `ColumnName` = 'Value'(您只能找到'Value‘和列内容完全相同的记录)
如果要查找ColumnName 包含该值的记录,则需要使用通配符:
WHERE `ColumnName` LIKE '%Value%' 如果您只想查找value的值以‘ColumnName’开头的记录(换句话说,前面不应该有任何内容),请使用:
WHERE `ColumnName` LIKE 'Value%' 示例
让我们考虑一下这个表(称为myTable):
ID | Description
----------------
1 | Lorem Ipsum dolar sit amet
2 | Lorem ipsum FooBar dolar sit amet
3 | FooBar
4 | Foo Bar现在这个查询:
SELECT *
FROM `myTable`
WHERE `Description` LIKE '%FooBar%'将返回第2行和第3行。第1行不会返回,因为它不包含“FooBar”。第4行不会被返回,因为它也不包含'FooBar‘。(它确实包含“Foo Bar”,但这是不同的)
现在让我们看看使用另一个查询会发生什么:
SELECT *
FROM `myTable`
WHERE `Description` LIKE 'FooBar%'(请注意, FooBar之前的% 已被删除)
该查询将只返回第3行,因为这是以FooBar开头的唯一行。
https://stackoverflow.com/questions/20499819
复制相似问题