执行where子句

时间:2013-03-14 08:23:59

标签: mysql sql select

我有这个客户表:

+----------+----------+---------+
| cliecode | cliename | deleted |
+----------+----------+---------+
|    00001 | ABC      |         |
|    00002 | DEF      |         |
|    00003 | GHI      | yes     |
|    00004 | JKL      |         |
+----------+----------+---------+

我正在执行这样的搜索查询:

SELECT * 
FROM client 
WHERE deleted ='' 
      AND cliecode LIKE '%$_POST[key]%' OR cliename LIKE '%$_POST[key]%' 

我想在deleted = ''条件之前先获取like。我怎样才能做到这一点?感谢。

2 个答案:

答案 0 :(得分:4)

然后将OR条件放在括号内,

select  * 
from    client 
where   deleted ='' and 
       (cliecode like '%$_POST[key]%' or cliename like '%$_POST[key]%')

答案 1 :(得分:0)

您是否在已删除字段中设置了任何默认值?

如果不是 -

然后你的查询应该是 -

select * from client 
where 
deleted is null 
and cliecode like '%$_POST[key]%' 
or cliename like '%$_POST[key]%'