EmreAltun
EmreAltun

Reputation: 423

how to select multiple names in a single query

I have a sql query which is like that

 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.

Upvotes: 4

Views: 46267

Answers (7)

Avantika Singh
Avantika Singh

Reputation: 1

  select *from users where uname = 'ali'
  union
  select *from users where uname = 'veli'

Upvotes: 0

SenthilPrabhu
SenthilPrabhu

Reputation: 659

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

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

Hope you got it..!

Upvotes: 2

user6620
user6620

Reputation: 111

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:

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

Upvotes: 1

Sathiya Raj S
Sathiya Raj S

Reputation: 41

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

Ex:

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

(or)

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

Upvotes: 4

Stuart Golodetz
Stuart Golodetz

Reputation: 20626

You can do:

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

As per:

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

Upvotes: 5

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79939

Try this:

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

Upvotes: 7

Maxim Krizhanovsky
Maxim Krizhanovsky

Reputation: 26699

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:

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

Upvotes: 3

Related Questions