galvin Posted April 16, 2010 Share Posted April 16, 2010 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 More sharing options...
Siann Beck Posted April 16, 2010 Share Posted April 16, 2010 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 Link to comment https://forums.phpfreaks.com/topic/198703-query-to-get-unique-info/#findComment-1042797 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.