Advertisements

SQL language (WHERE clause)

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

Offline
Irin
Posts: 2
Joined: Mon Jan 11, 2010 8:53 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?

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

Offline
tutorialspoint
Site Admin
Posts: 143
Joined: Fri Apr 27, 2007 6:52 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.

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

Offline
Irin
Posts: 2
Joined: Mon Jan 11, 2010 8:53 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!

mysql server clause

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

Offline
nameranjeet
Posts: 3
Joined: Wed Dec 28, 2011 6:18 pm
Location: India
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?

I don't get it

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

Offline
remotedba
Posts: 7
Joined: Mon Jan 16, 2012 10:20 pm
There is meaning of OR and AND fuction.

Re: SQL language (WHERE clause)

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

Offline
ajitpaswan
Posts: 11
Joined: Mon Dec 24, 2012 2:41 am
Location: India
SELECT Index FROM Table_1
WHERE Name IN( 'A' , 'D');