妙网科技 妙网科技 首页 妙网科技 网站开发 妙网科技 php SQL语句有and也有or应该怎么写?

php SQL语句有and也有or应该怎么写?

所属栏目: 网站开发 | 更新时间:2016-8-14 | 阅读:2995 次
用小括号()包含就可以区别开。
例如:
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的数据。
就像四则运算加上小括号就有了计算优先原则。