![]() |
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, |
Quote:
Quote:
|
??? 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, |
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.
|
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??? |
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:
|
I was replying to BubikolRamios
Basically what I'm trying to do is populate a (html) table like this: Code:
User | ItemsCode:
User | Items |
> 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 > 73So move that to the ON, thus: Code:
SELECT a.username, |
thank you so much. So sorry I didn't add those...
|
The only exception to that rule is that you *can* make tests for IS NULL or IS NOT NULL in the WHERE clause.
|
Quote:
|
Yep, that query fixed everything. Thank you very much, once again! Learn something new every day!
|
| All times are GMT +1. The time now is 01:59 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.