SQL language (WHERE clause)

Discussion on SQL and MySQL
no avatar
Irin
 
Posts: 2
Joined: Mon Jan 11, 2010 8:53 pm

SQL language (WHERE clause)

by Irin » Mon Jan 11, 2010 9:06 pm

Hi Everyone!

I have a Table_1 with columns "Index"and "Name" that looks like this:

Index Name
1 A
1 B
1 C
1 D
-----------
2 A
2 B
2 E
2 G
2 D
-----------
3 D
3 M
-----------
4 A
4 B
4 D
4 M
..........

I need to select from the table all the indices where "Name" contain both A and D. So I need to make query like:

Code: Select all
SELECT Index FROM Table_1
WHERE Name = 'A' AND Name = 'D'

which would give me indices 1, 2, 4.

There is a construction in the language that uses "OR" instead of "AND", but it does not return what I need.

Can anyone help me to construct the query for getting the information I need?

no avatar
tutorialspoint
Site Admin
 
Posts: 144
Joined: Fri Apr 27, 2007 6:52 pm

by tutorialspoint » Wed Jan 13, 2010 4:39 pm

Are these 15 records you mentioned over here or what? I did not understand how you would fetch rows having A and D using AND. In this case you should use OR then it would return you all the rows having A or D.

SELECT Index FROM Table_1
WHERE Name = 'A' OR Name = 'D'
Moderator, TP
Keep visiting and share this site with your friends.

no avatar
Irin
 
Posts: 2
Joined: Mon Jan 11, 2010 8:53 pm

by Irin » Fri Jan 15, 2010 5:36 pm

Thank you for the reply :)
This 'OR' does not work for me, because I need only those indices which contain the both A and D.
I seems the query should be:

Code: Select all
SELECT Indx
FROM Table_1
WHERE name IN('A','D')
GROUP BY Indx
HAVING COUNT(DISTINCT name) >= 2


And it works!

no avatar
nameranjeet
 
Posts: 3
Joined: Wed Dec 28, 2011 6:18 pm
Location: India

mysql server clause

by nameranjeet » Wed Dec 28, 2011 6:45 pm

Hi Everyone!

I have a Table_1 with columns "Index"and "Name" that looks like this:

Index Name
1 A
1 B
1 C
1 D
-----------
2 A
2 B
2 E
2 G
2 D
-----------
3 D
3 M
-----------
4 A
4 B
4 D
4 M
..........

I need to select from the table all the indices where "Name" contain both A and D. So I need to make query like:

Code:
SELECT Index FROM Table_1
WHERE Name = 'A' AND Name = 'D'

which would give me indices 1, 2, 4.

There is a construction in the language that uses "OR" instead of "AND", but it does not return what I need.

Can anyone help me to construct the query for getting the information I need?

no avatar
remotedba
 
Posts: 7
Joined: Mon Jan 16, 2012 10:20 pm

I don't get it

by remotedba » Mon Jan 16, 2012 10:26 pm

There is meaning of OR and AND fuction.

no avatar
ajitpaswan
 
Posts: 11
Joined: Mon Dec 24, 2012 2:41 am
Location: India

Re: SQL language (WHERE clause)

by ajitpaswan » Fri May 31, 2013 3:51 am

SELECT Index FROM Table_1
WHERE Name IN( 'A' , 'D');

Return to SQL and MySQL