Jump to content

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

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.