Jump to content

[SOLVED] Need help with a query


skyer2000

Recommended Posts

Here is my table:

 

key_table

keyid  papid  userid
4 	7 	0
2 	1 	0
1 	2 	0
2 	2 	0
3 	2 	0
6 	2 	0
7 	19 	0
3 	19 	0
2 	3 	0
1 	0 	1
2 	0 	1
2 	0 	2
3 	0 	2

 

What I'm trying to figure out is how to setup a query that will check for a papid (lets say 2), get the keyid's associated to that papid (which would be 1,2,3,6), then pull all userid's that also have that keyid (which would output 1,1,2,2).

 

Any ideas on how to setup a query to do this?

Link to comment
https://forums.phpfreaks.com/topic/158616-solved-need-help-with-a-query/
Share on other sites

Hi

 

Quick play (untested)

 

Crude method:-

 

SELECT userid
FROM key_table
WHERE keyid IN 
(SELECT keyid
FROM key_table
WHERE a.papid = 2)

 

More elegant method

 

SELECT b.userid
FROM key_table a
JOIN key_table b 
ON a.keyid = b.keyid
WHERE a.papid = 2

 

All the best

 

Keith

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.