SQL 如何在单个查询中选择多个名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10064294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 15:14:24  来源:igfitidea点击:

how to select multiple names in a single query

sql

提问by EmreAltun

I have a sql query which is like that

我有一个类似的 sql 查询

 select * 
 from users 
 where uname="ali" and uname="veli"

but it is not worked what ? want to do is get the person whose name is "ali" and "veli" but if "ali" is not exist and "veli" is exist, the existed ones should be return. How can I do this. it is easy query but I am confusing.

但它不起作用什么?想要做的是获取名为“ali”和“veli”的人,但如果“ali”不存在而“veli”存在,则应返回存在的人。我怎样才能做到这一点。这很容易查询,但我很困惑。

回答by Mahmoud Gamal

Try this:

尝试这个:

 SELECT * 
 FROM users 
 WHERE uname IN ("ali", "veli")

回答by Stuart Golodetz

You can do:

你可以做:

select * from users where uname in ("ali", "veli")

As per:

按照:

http://www.w3schools.com/sql/sql_in.asp

http://www.w3schools.com/sql/sql_in.asp

回答by Sathiya Raj S

You can use OR clause or IN clause in you sql.

您可以在 sql 中使用 OR 子句或 IN 子句。

Ex:

前任:

SELECT * FROM users WHERE uname='ali' OR uname='veli'

(or)

(或者)

SELECT * FROM users WHERE uname IN ('ali','veli');

回答by Maxim Krizhanovsky

name cannot be both ali and veli at the same time. What you want to do is to get users where name is either ali or veli, the query would look like this:

name 不能同时是 ali 和 veli。您想要做的是获取名称为 ali 或 veli 的用户,查询将如下所示:

select * from users where (uname="ali" or uname="veli")

回答by SenthilPrabhu

If you need both the condition to be satisfied then use:
select * from users where uname="ali" and uname="veli"

如果您需要同时满足两个条件,请使用:
select * from users where uname="ali" and uname="veli"

If you want records where eithercondition is satisfied use:
select * from users where uname="ali" or uname="veli"

如果您想要满足任一条件的记录,请使用:
select * from users where uname="ali" or uname="veli"

Hope you got it..!

希望你明白了..!

回答by user6620

You need to provided the proper logic for the where clause. Currently you are asking for the record where the entry uname is both 'ali' and 'veli' as each row will only have one entry for uname this will return nothing. The proper logic for this query is 'OR' as it tells your SQL engine to find all row where uname = 'ali' OR 'veli'. Your query will end up looking like the following:

您需要为 where 子句提供正确的逻辑。当前,您正在请求条目 uname 既是“ali”又是“veli”的记录,因为每行只有一个 uname 条目,这将不会返回任何内容。此查询的正确逻辑是“OR”,因为它告诉您的 SQL 引擎查找 uname = 'ali' OR 'veli' 的所有行。您的查询最终将如下所示:

SELECT * FROM users WHERE uname='ali' OR uname='veli';