Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-04-2013, 08:50 PM   PM User | #1
Kurisvo
New Coder

 
Join Date: Apr 2011
Posts: 59
Thanks: 8
Thanked 0 Times in 0 Posts
Kurisvo is an unknown quantity at this point
working with GROUP_CONCAT error

So I've been practicing with random queries and I've run into a problem

First off, here is my query:

Code:
SELECT a.username,
                 GROUP_CONCAT(items.id) AS itemslist,
                
         FROM a LEFT JOIN items ON items.owner = a.username          
         GROUP BY a.username
and that works perfectly for what I need (I need the items in a list, like the group_concat function gives me), except when there are no records for that user in the items table. I'm wondering if you can help me write a query that would work both ways? So it would return the list of users from the a table, no matter if they have items or not.
Kurisvo is offline   Reply With Quote
Old 02-04-2013, 09:21 PM   PM User | #2
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
Quote:
LEFT JOIN
Quote:
So it would return the list of users from the a table, no matter if they have items or not.
left join does exactly that.
__________________
Found a flower or bug and don't know what it is ?
agrozoo.net galery
if you don't spot search button at once, there is search form:
agrozoo.net galery search
BubikolRamios is offline   Reply With Quote
Old 02-04-2013, 09:40 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
??? That works fine, even with no records in items for a given record in a.

You will get NULL for the GROUP_CONCAT, of course.

Is the problem than you don't know what to do with the NULL in your PHP code?

If you want to provide a default value, in place of the NULL, just do this:
Code:
SELECT a.username,
       IFNULL( GROUP_CONCAT(items.id), '--none--' ) AS itemslist
FROM a LEFT JOIN items ON items.owner = a.username          
GROUP BY a.username
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 02-04-2013, 09:40 PM   PM User | #4
Kurisvo
New Coder

 
Join Date: Apr 2011
Posts: 59
Thanks: 8
Thanked 0 Times in 0 Posts
Kurisvo is an unknown quantity at this point
Except a left join doesn't return the information in the format I need. Which is a list of the ids of the items with commas separating them. Such as 1,5,6,78.
Kurisvo is offline   Reply With Quote
Old 02-04-2013, 09:43 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Your statement makes no sense.

If there are no matching items, then HOW CAN THERE POSSIBLY BE any list?

If "john" has no matching records in the items table, then how can you expect to get a list of ids for "john" when there are no ids for him???
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 02-04-2013, 09:50 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You need to choose what to do with it. You can either have a list of all a.username, and only matching itemslist within it (including NULL where no matching list exists), or no record of a.username if there is no matching itemslist.
I can't see what you are doing with a comma separating list that cannot be accommodated for using null instead. Personally I wouldn't use a group_concat, but that's just me. With the comma string, and a language like PHP, you'd execute an explode() call and it will return an array of each item. Problem is empty strings and nulls are considered a single character (that is, null), which can be exploded resulting in a single item of empty string.
PHP Code:
$aList = array();
if (!empty(
$string))
{
    
$aList explode(','$string);

Simple as that.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 02-04-2013, 09:52 PM   PM User | #7
Kurisvo
New Coder

 
Join Date: Apr 2011
Posts: 59
Thanks: 8
Thanked 0 Times in 0 Posts
Kurisvo is an unknown quantity at this point
I was replying to BubikolRamios

Basically what I'm trying to do is populate a (html) table like this:

Code:
User | Items
So then with some info it would look like

Code:
User | Items
        ___________
        John   | 1,5,6
        James | 5,7,8,1
        Megan | None
        Tim    | 5,2,3
However when I run the query, Megan isn't returned because she doesn't have any item records. Would the WHERE statements I have limiting what kind of items being returned mess it up?
Kurisvo is offline   Reply With Quote
Old 02-04-2013, 10:05 PM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
> Would the WHERE statements I have limiting what kind of items being returned mess it up?

YES!!!

You can *NOT* use WHERE with the *dependent* table! (That is, the right side table in a LEFT join. It would be the left side table in a RIGHT join, of course.)

If you do, you CONVERT the LEFT JOIN (or RIGHT JOIN) to an INNER JOIN!!!!

Any conditions that you want to put on the dependent table *MUST* be part of the ON condition and *NOT* be in the WHERE!

Let's say you currently have WHERE items.size > 73

So move that to the ON, thus:
Code:
SELECT a.username,
       IFNULL( GROUP_CONCAT(items.id), '--none--' ) AS itemslist
FROM a LEFT JOIN items 
ON items.owner = a.username AND items.size > 73
GROUP BY a.username
Next time, show us the entire query and we'll be able to help you better.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
Kurisvo (02-04-2013)
Old 02-04-2013, 10:06 PM   PM User | #9
Kurisvo
New Coder

 
Join Date: Apr 2011
Posts: 59
Thanks: 8
Thanked 0 Times in 0 Posts
Kurisvo is an unknown quantity at this point
thank you so much. So sorry I didn't add those...
Kurisvo is offline   Reply With Quote
Old 02-04-2013, 10:07 PM   PM User | #10
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
The only exception to that rule is that you *can* make tests for IS NULL or IS NOT NULL in the WHERE clause.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 02-04-2013, 10:12 PM   PM User | #11
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
Originally Posted by Kurisvo View Post
thank you so much. So sorry I didn't add those...
Hey, I give you a lot of credit for asking the right question about the WHERE clause. I can't tell you how many times I have run into this where the coder had no clue at all. You have the right instincts! Congratulations.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 02-04-2013, 10:18 PM   PM User | #12
Kurisvo
New Coder

 
Join Date: Apr 2011
Posts: 59
Thanks: 8
Thanked 0 Times in 0 Posts
Kurisvo is an unknown quantity at this point
Yep, that query fixed everything. Thank you very much, once again! Learn something new every day!
Kurisvo is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:49 AM.


Advertisement
Log in to turn off these ads.