This question already has answers here:
What is the difference between "INNER JOIN" and "OUTER JOIN"?
(28 answers)
Closed 8 years ago.
Im doing the follwing, to create an user report
SELECT b.username, b.name, b.permissiontoedit, a.total, a.user
FROM (SELECT user, Count( * ) AS total
FROM products
GROUP BY user)a
JOIN user b ON a.user = b.username
This should give a table with the username, full name, permision (1/0) and the total of entries.
Sadly, the query does only list users, which made more 1 or more entries in the table products. But i want all users, and if the have not made any entries in products it should display 0 or nothing.
where do i made a mistake?
Using a LEFT JOIN you can get your result:
SELECT
u.username, u.name, u.permissiontoedit, COUNT(p.user) as total
FROM
user u
LEFT JOIN
products p
ON
u.username = p.user
Note: COUNT(expression) counts only NOT NULL rows in contrast to COUNT(*) that counts every row.
Related
This question already has answers here:
MYSQL - Select only if row in LEFT JOIN is not present
(2 answers)
Closed 2 years ago.
I have a mysql problem that I can't find a solution.
I have two tables:
groups (group_name, group_id)
members (member_id, group_id, user_id)
I want to select all groups that don't have a match for a certain user_id in the members table.
The reason for that is because I want to show to the user all groups that he's not a part of.
If there is also a way to display the number of members in each group (by counting the number of matches in members table) it would be great.
Thanks in advance
You can use not exists:
select g.*
from groups g
where not exists (
select 1
from group_members gm
where gm.group_id = g.group_id and gm.user_id = ?
)
The question mark should be replaced with the specific user that you want to use as parameter.
Note that this also brings groups that have no members at all.
If you want the user count per group too, then you can join and filter with a having clause:
select g.group_id, g.group_name, count(gm.group_id) no_members
from groups g
left join group_members gm on gm.group_id = g.group_id
group by g.group_id, g.group_name
having count(gm.user_id) = 0 or max(gm.user_id = ?) = 0
Or if you want to evict groups without members:
select g.group_id, g.group_name, count(*) no_members
from groups g
inner join group_members gm on gm.group_id = g.group_id
group by g.group_id, g.group_name
having max(gm.user_id = ?) = 0
This question already has answers here:
Retrieving the last record in each group - MySQL
(33 answers)
Closed 3 years ago.
I'm coding a message system using mysql.
Everything works fine when I list users whom I'm conversing with, until I want to add date of the last or the start of conversation.
When I add a.date I get duplicate results when the date isnt the same.
Here is my sqlfiddle
Since, you were pulling only user_id then in both cases (send/recieve) it was giving you distinct record. But now with date it is no more distinct. you need to do something like:
SELECT temp.id_user, MAX(temp.date) as date
FROM
(
SELECT users.id_user,
a.date
FROM users
LEFT JOIN message AS a
ON users.id_user = a.id_user_recipient
LEFT JOIN message AS b
ON a.id_user_recipient = b.id_user_sender
WHERE a.id_user_sender = 1
UNION DISTINCT
SELECT users.id_user,
a.date
FROM users
LEFT JOIN message AS a
ON users.id_user = a.id_user_sender
LEFT JOIN message AS b
ON a.id_user_sender = b.id_user_recipient
WHERE a.id_user_recipient = 1
) as temp
GROUP BY temp.id_user;
Grabbing max(date) will ensure to return only one record as with group by
This question already has answers here:
Select all records don't meet certain conditions in a joined table
(2 answers)
Closed 5 years ago.
i am having two tables in my DB like members and payments . Table Members has the name and id of users and payment table has the id,amount and session of payments like if user 1 has paid 1500 for a session 3 then table payment has the following details. mid 1, session 3 and amount 1500.
Now i want to fetch the names of all the users which have not aid for session 1
i am using the following query but it is not working
SELECT NAME
,id
FROM member m
,payment p
WHERE (
p.session = '3'
AND m.id != p.mid
)
This is not giving me the required result please help me .
What about something like this:
SELECT NAME
,id
FROM member m
inner JOIN payment paid
ON m.id = paid.mid
AND paid.sessionid = '1'
LEFT JOIN payment p
ON m.id = p.mid
AND p.sessionid = '3'
WHERE p.id IS NULL
When you want to get data from more than 2 tables , you have to use join and if you don't want to use join as you are doing, then make sure you have exact relation between that tables, e.g a.id = b.membe_id etc..
And in your case, i think the relation is not right, make sure you have something common in two tables.
Thanku.
This question already has answers here:
conditional join in mysql
(4 answers)
Closed 6 years ago.
I have a little complicated query here. I have to select columns by names cant just user 'orders.*. because i have multiple columns from different tables with the same name.
What I'm trying to do is to select specific fields from orders,users,payment_methods_translation and join bank_accounts_translation only if the orders.payment_method_id = '3'
SELECT
orders.id as orderid,
orders.final_total,
orders.user_id,
orders.auto_cancel,
users.id as userid,
users.first_name,
payment_methods_translation.payment_method_id,
payment_methods_translation.name
FROM
orders,users,
payment_methods_translation
WHERE
orders.id='$id' AND
orders.user_id = users.id AND
orders.payment_method_id = payment_methods_translation.payment_method_id AND
orders.auto_cancel='1'
JOIN
bank_accounts_translation ON (orders.payment_method_id='3'
AND orders.bank_id = bank_accounts_translation.bank_account_id)
But I get a mysql error. So how can I select all the fields from bank_accounts_translation only if orders.payment_method_id = '3' and orders.bank_id = bank_accounts_translation.bank_id
Left join should do it... I
Refactored the code to use the current ANSI standards, you appear to be mixing them.
used table aliases to improve readability.
LEFT JOIN says return all records from prior listed tables and only those that match the on criteria of the table being joined to.
SELECT O.id as orderid
,O.final_total
,O.user_id
,O.auto_cancel
,u.id as userid
,u.first_name
,PMT.payment_method_id
,PMT.name
FROM orders O
INNER JOIN users U
ON O.user_id = U.id
INNER JOIN payment_methods_translation PMT
ON O.payment_method_id = PMT.payment_method_id
LEFT JOIN bank_accounts_translation BAT
ON O.payment_method_id='3'
and O.bank_id = BAT.bank_account_id
WHERE O.id='$id'
and O.auto_cancel='1'
I have a mysql DB with two tables:
messages
message_answers
I'd like to fetch all messages and the number of answers for each of them, like:
first message (10 answers)
second message (5 answers)
Is it possible with a single sql query ?
I tried a query and a subquery for the count, but I don't know how to have the current id from the main query to make the "WHERE" restriction on the subquery (like: "message_answers.message_id = messages.id").
Thank you,
Sébastien
SELECT m.message, COUNT(ma.answer_id) AS AnswerCount
FROM messages m
LEFT JOIN message_answers ma
ON m.id = ma.message_id
GROUP BY m.message
select m.id, count(ma.id)
from messages m
join join message_answer ma on ma.id = m.message_id
group by 1