Forum Moderators: open

Message Too Old, No Replies

matching rows

matching rows

         

quackman

2:02 pm on Mar 11, 2009 (gmt 0)

10+ Year Member



Following a select statement I get results similar to the following:

Facility used_for
-------- ---------
abc parties
abc basketball
abc water fights
def parties
def boxing
def exercise
def track and field

Is there a way to query the database and find facilities that can be used for parties and waterfights?

coopster

2:23 pm on Mar 11, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, quackman.

Yes, you can use operators such as LIKE in your WHERE clause.

quackman

2:55 pm on Mar 11, 2009 (gmt 0)

10+ Year Member



Thank you for your quick response.

Sorry about the typo when I typed "waterfights", I meant "water fights". I think this may have confused the issue. "Like" does not get me to where I need to go.

I can select facilities that have parties or water fights with a simple or statement. However that doesn't always give me facilities that have both. It may provide me with facilities that only have one of the two uses.

If I use an and statement, it will always fail because "used_for" cannot equal 2 different values in the same row.

What I need is to be able to find 2 rows where the facilities are the same (i.e. abc) and the used_for in row x is "parties" and used_for in row y is "water fights"

mattur

3:09 pm on Mar 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



SELECT 
*
FROM
MyTable
WHERE
Facility = 'abc'
AND (used_for ='parties' OR used_for ='water fights');

quackman

3:27 pm on Mar 11, 2009 (gmt 0)

10+ Year Member



That query returns facilities that have one or the other or both. I only want the ones that have both.

LifeinAsia

3:30 pm on Mar 11, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



SELECT *
FROM MyTable
WHERE Facility IN (SELECT Facility FROM MyTable WHERE used_for = 'parties') AND Facility IN (SELECT Facility FROM MyTable WHERE used_for = 'water fights')

quackman

3:43 pm on Mar 11, 2009 (gmt 0)

10+ Year Member



Awesome.. Thanks everyone. That is exactly what I was looking for.

Demaestro

5:05 pm on Mar 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Maybe throw a DISTINCT on that query as well.

I am not sure of your data but it may be possible that a duplicate rows can be returned with that query.

[edited by: Demaestro at 5:05 pm (utc) on Mar. 11, 2009]