Jump to content

Query to get unique info


galvin

Recommended Posts

Ok, I have two tables of concern...

 

users: (with a field for userid, among others)

pools: (with a field for poolid and poolname, among others)

picks: (with a field for poolid and userid, among others)

 

How can I write a MYSQL query that says in plain english..."Bring me back each unique "poolname" that this particular user has submitted picks for"?

 

For example, if user 1 has submitted one set of picks for "Bob's Pool", four sets of picks for "Mark's Pool" and three sets of picks for "Tony's Pool", I would want my query results to be:

 

Bob's Pool

Mark's Pool

Tony's Pool

 

The key is that I want each unique poolname just once.

 

I was messing with the following, but it's repeating the same pool name more than once...

$sql = "SELECT poolname, pools.poolid FROM pools, picks
		WHERE picks.userid = 1 
		AND picks.poolid = pools.poolid";

 

 

Can anyone help?  I'm wondering if I need to be doing some sort of "JOIN" instead, which I'm not too good at yet  :-\

Link to comment
https://forums.phpfreaks.com/topic/198703-query-to-get-unique-info/
Share on other sites

Actually, you're already doing a join, just not explicitly, and your join is backward (sort of). But that's beside the point, what you need is the GROUP BY clause. Try this:

SELECT poolname, pools.poolid
FROM   picks, pools
WHERE  picks.userid = 1
  AND  pools.poolid = picks.poolid
GROUP  BY picks.poolid

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.