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.
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
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.
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.
__________________
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
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?
> 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.
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.