Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.