MySQL Forums
Forum List  »  Newbie

Re: Doing an 'AND' select on a two field table
Posted by: Felix Geerinckx
Date: June 14, 2005 07:51AM

Rob Lee wrote:
> How can I select rows from a table that match a series of conditions:
>
> My table is like this:
>
> char_voice_id, voice_id, attr_id
> 1,1,1
> 2,1,2
> 3,1,3
> 4,2,4
> 5,2,7
> 6,3,1
> 7,3,2
> 8,3,3
>
>
> I want to select the rows where the attr_id matches, say, two or more values. For example, I
> want attr_id =1, 2 and 3. Which means I want voice_id 1 and 3 returned. Get it?

One way:

SELECT
voice_id
FROM char_voice
WHERE
attr_id IN (1,2,3)
GROUP BY voice_id
HAVING COUNT(*) = 3; # 3 = number of distinct attr_id's in IN-list

This method relies on a UNIQUE constraint for (voice_id, attr_id).

--
felix
Please use BBCode to format your messages in this forum.

Options: ReplyQuote


Subject
Written By
Posted
Re: Doing an 'AND' select on a two field table
June 14, 2005 07:51AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.