PHP MySQL question

This is a discussion on PHP MySQL question within the PHP Language forums, part of the PHP Programming Forums category; Hi everyone, I have created a table with the following 3 columns ID UserID SkillID 1 2 1 1 2 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-18-2005
Maziar Aflatoun
 
Posts: n/a
Default PHP MySQL question

Hi everyone,

I have created a table with the following 3 columns

ID UserID SkillID
1 2 1
1 2 3
1 2 2
1 3 2

Is there a way to return all UserIDs that match SkillID=1 and SkillID=2 and
SkillID=3? (not OR)

Thank you
Maziar A.


Reply With Quote
  #2 (permalink)  
Old 05-18-2005
Samuel (lets dot monroe at gmail dot com)
 
Posts: n/a
Default Re: PHP MySQL question

If 1, 2 and 3 are the _only_ allowed values for SkillID you can...

SELECT ID, UserID, count(SkillID) FROM <TableName> GROUP BY ID, UserID

Then those rows with a 3 in the third column are what you're looking
for.

May be wrong, my sql is a little rusty lately :D

Reply With Quote
  #3 (permalink)  
Old 05-18-2005
David Mackenzie
 
Posts: n/a
Default Re: PHP MySQL question

On 18 May 2005 09:09:43 -0700, "Samuel (lets dot monroe at gmail dot
com)" <lets.monroe@gmail.com> wrote:

>If 1, 2 and 3 are the _only_ allowed values for SkillID you can...
>
>SELECT ID, UserID, count(SkillID) FROM <TableName> GROUP BY ID, UserID
>
>Then those rows with a 3 in the third column are what you're looking
>for.


Use HAVING to filter on the result of count():

SELECT ID, UserID, count(SkillID) FROM <TableName> GROUP BY ID, UserID
HAVING count(SkillID)=3

This also assumes a unique constraint on UserID and SkillID preventing
the same SkillID appearing multiple times for one UserID.

--
David ( @priz.co.uk )
Reply With Quote
  #4 (permalink)  
Old 05-18-2005
ECRIA Public Mail Buffer
 
Posts: n/a
Default Re: PHP MySQL question

SELECT UserID FROM tablename WHERE UserID IN (SELECT UserID FROM tablename
WHERE SkillID=1) AND UserID IN (SELECT UserID FROM tablename WHERE
SkillID=2) AND UserID IN (SELECT UserID FROM tablename WHERE SkillID=3)

However, it may be more efficient for you to redesign your database.

ECRIA
http://www.ecria.com


Reply With Quote
  #5 (permalink)  
Old 05-18-2005
BKDotCom
 
Posts: n/a
Default Re: PHP MySQL question

This would be a mysql question... try the mysql group..
I'm not sure, but I think this would be a self join

SELECT *
FROM table, table as table_copy
WHERE table.UserID = table_copy.UserID
AND table.SkillID = 1
AND table.SkillID = 2
AND table.SkillID = 3

or maybe something like:

SELECT UserID, SkillID,
GROUP_CONCAT(SkillID ORDER BY test_score DESC SEPARATOR ' ') as skills
FROM table
WHERE skills = '1 2 3'
GROUP BY UserID

Reply With Quote
  #6 (permalink)  
Old 05-18-2005
Samuel (lets dot monroe at gmail dot com)
 
Posts: n/a
Default Re: PHP MySQL question


David Mackenzie ha escrito:

> This also assumes a unique constraint on UserID and SkillID

preventing
> the same SkillID appearing multiple times for one UserID.


Yep, I assumed that from the fact that (as far as we know anyway) the
table has only three columns, so if ID and UserID can repeat SkillID
can't :)

Reply With Quote
  #7 (permalink)  
Old 05-18-2005
Geoff Berrow
 
Posts: n/a
Default Re: PHP MySQL question

I noticed that Message-ID: <d6fqk2$kjl$1@murdoch.acc.Virginia.EDU> from
ECRIA Public Mail Buffer contained the following:

>SELECT UserID FROM tablename WHERE UserID IN (SELECT UserID FROM tablename
>WHERE SkillID=1) AND UserID IN (SELECT UserID FROM tablename WHERE
>SkillID=2) AND UserID IN (SELECT UserID FROM tablename WHERE SkillID=3)



Can you do that in MySQL now?
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Reply With Quote
  #8 (permalink)  
Old 05-19-2005
Dave
 
Posts: n/a
Default Re: PHP MySQL question

Geoff Berrow (blthecat@ckdog.co.uk) decided we needed to hear...
> >SELECT UserID FROM tablename WHERE UserID IN (SELECT UserID FROM tablename
> >WHERE SkillID=1) AND UserID IN (SELECT UserID FROM tablename WHERE
> >SkillID=2) AND UserID IN (SELECT UserID FROM tablename WHERE SkillID=3)

>
> Can you do that in MySQL now?
>

IIRC sub-selects came in at v4.1

--
Dave <dave@REMOVEbundook.com>
(Remove REMOVE for email address)
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 11:50 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0