如何使用 OR &AND 在 mysql 的 WHERE 子句中 [英] How to use OR & AND in WHERE clause in mysql

查看:48
本文介绍了如何使用 OR &AND 在 mysql 的 WHERE 子句中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Mysql 的新手 &有一个表,目前我正在获取数据为

I'm new to Mysql & have a table where at presently i'm fetching data as

SELECT * FROM tablename WHERE name='$user' AND date='$selecteddate'

现在我想再添加 1 个名为 status_id 的列,我想从中选择值,即 1 或 2

Now i want to add 1 more column named status_id where i want to select from to values i.e 1 or 2

我试过这个查询

 SELECT * FROM tablename WHERE (name='Ankit') AND (date='2015-04-23') AND (status_id='1' OR status_id='2')

但没有成功.

请帮忙.

推荐答案

嗯,你只需要 or 子句所关注的元素在括号.

Well, you just need the elements concerned by the or clause to be between parenthesis.

顺便说一下,它们不是需要的"(您可以使用 AND/OR 的优先顺序),但我会说这是提高可读性的最佳方式.

They are not "needed", by the way (you could use precedence order of AND / OR), but I would say this is the best way for readability.

如果 status_id 数据类型为 int,则不需要 ' ' 1 和 2 左右.

And if status_id data type is int, you don't need the ' ' around 1, and 2.

SELECT * 
 FROM tablename 
WHERE name='Ankit' 
AND date='2015-04-23' 
AND (status_id=1 OR status_id=2) 

顺便说一句,在这种情况下,您可以使用 IN 子句

By the way, in this case, you may use an IN clause

AND status_id in (1, 2)

这篇关于如何使用 OR &AND 在 mysql 的 WHERE 子句中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆