php SQL语句有and也有or应该怎么处理呢?

来源:百度知道 编辑:UC知道 时间:2024/07/01 01:32:31
如ASP是这样的 where (a=1 or a=2) and b=3

也就是三个条件,有两个是可选条件,一个是必须选条件。
请问PHP的格式是怎么样的呢?

用小括号()包含就可以区别开。
例如:
select * from table where title like '%hello%' and (contents like '%good%' or contents like '%ok%')
sql语句where部分解释如下:
title like '%hello%' and (contents like '%good%' or contents like '%ok%')
title 字段模糊查询包含 hello 字符串的数据,并且 contents 字段模糊查询包含 good 字符串的数据,或者contents 字段模糊查询包含 ok 字符串的数据
比如数据表数据如下:
字段 id --- title --- contents
数据 1 --- 11hello22 --- yougoodss
2 --- aaahello333 --- fdffokssfff
3 --- bbbhello666 ---- fffaafdafa1
像上面的数据sql语句会同时查询出1、2的数据。

就像四则运算加上小括号就有了计算优先原则。

一样的写法 只是PHP的标准的写法是 where (`a`=1 or `a`=2) and `b`=3 这里的a b 为数字库字段名.所以要用那个符号.有时候不用也不出错.但为了更能适用多个平台.所以建议还是写上好点.

php里面,or跟||优先级别是不同的,你可以写成
where(a=1||a=2 and b=3);

如何是sql语句基本上都是一样的,跟PHP和asp没有关系 ,建议写标准格式