MySQL语句之or/and
发布日期:2021-05-04 09:23:49 浏览次数:19 分类:精选文章

本文共 1024 字,大约阅读时间需要 3 分钟。

在where中可以包含任意数目的and和or操作符,在没有任何其他符号的时候,

例如括号,SQL会首先执行and条件,然后才执行or语句

eg.    select * from table from id=1 or id=2 and price>=10;

这条语句默认执行的是id=2并且price大于等于10的,或者是id=1。

如果加上括号:select * from table from (id=1 or id=2) and price>=10;

则这条语句执行的是id=1或id=2,并且price大于等于10。

mysql 允许使用多个where子句,组合where子句允许使用两种方式使用:AND 和OR子句的方式使用. 

数据库中的操作符号:AND , OR , IN , NOT.

AND: 

SELECT * FROM products WHERE products.vend_id = 1003 AND products.prod_price <= 10;

OR: 

SELECT * FROM products WHERE products.vend_id = 1002 OR products.vend_id = 1003 ;

IN: 

建议能使用IN的子句中不使用OR,IN行性能好,方便理解. 
SELECT * FROM products WHERE products.vend_id IN (1002,1003);

NOT: 

Mysql对NOT的支持仅在对IN,BETWEEN,EXISTS子句取反,这与其他多数数据库对各种条件都支持不同. 
SELECT * FROM products WHERE products.vend_id NOT IN (1002,1003);

注意: 

在同时有AND和OR的子句中,mysql是优先处理AND操作的.一般建议使用()来确定处理顺序和消除歧义. 
比如: SELECT * FROM products WHERE (products.vend_id= 1002 OR products.vend_id=1003) AND prod_price >= 10;

转载:

http://blog.csdn.net/stypace/article/details/38346351

http://blog.csdn.net/u011479200/article/details/78513358

上一篇:Uncaught TypeError: l.push is not a function
下一篇:php返回当前日期或者指定日期是周几

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2025年03月28日 01时33分56秒