I have two MySQL tables users and task_reg
users
id name
1 user1
2 user2
3 user3
4 user4
5 user5
6 user6
7 user7
8 user8
9 user9
task_reg
top_id use_id payment
1 1 1
1 2 1
1 4 1
2 3 1
2 5 1
I have to display all the users and also show whether they have paid or not
here is my SQL command but it only shows the paid users
SELECT users.id, users.name, task_reg.payment
FROM task_reg
JOIN users ON users.id = task_reg.use_id
JOIN topics ON topics.id = task_reg.top_id
WHERE topics.id = 1
Result
id name payment
1 user1 1
2 user2 1
4 user4 1
Expected result
id name payment
1 user1 1
2 user2 1
3 user3 0
4 user4 1
5 user5 0
6 user6 0
7 user7 0
8 user8 0
9 user9 1
DB-Fiddle
Thanks.
Since you want all the users, it should be your first table in the join order.
Change your Inner join to Left Join.
Where condition on the topics.id should be a Join condition.
Use Coalesce() function to get value of payment done as 0 (because of no matching rows found).
Try:
SELECT users.id, users.name, COALESCE(task_reg.payment, 0) AS payment
FROM users
LEFT JOIN task_reg ON users.id = task_reg.use_id
LEFT JOIN topics ON topics.id = task_reg.top_id AND topics.id = 1
Related
I have a user table
id
name
number
created_at
1
User1
102
2022-02-03
2
User2
103
2022-02-06
3
User3
123456
2022-02-07
4
User4
1234567
2022-02-07
5
User5
1234568
2022-04-08
and invite table
id
invited_by
invited_to_number
created_at
1
1
123456
2022-04-05 12:03:03
2
2
123456
2022-04-05 17:13:23
2
1
1234567
2022-04-07 17:13:23
2
1
1234568
2022-04-07 17:13:23
2
2
1234565
2022-04-08 17:13:23
I need user table records along with the count of all invited registered in
all-time accepted in
Today accepted in
yesterday accepted in
with the group by number using MySQL Query
Count will only go to those users who were invited first
and after successfully registered count will add to the invited the user
So, the Expected result is
Today Date: 2022-04-08
id
name
all_time_accepted
Today's accepted
yesterday accepted
1
User1
3
1
1
2
User2
0
0
0
3
User3
0
0
0
4
User4
0
0
0
5
User5
0
0
0
If you want to count only today's or yesterday's invites, place a boolean condition (like DATE(i.created_at) = #today_date) inside the select expression. The condition will evaluate to 1 (if true) or 0 (if false), which means that if you SUM(), you will get the number of rows satisfying this condition.
SELECT
u.id,
u.name,
SUM(
u1.id IS NOT NULL
AND i1.id IS NULL
) AS all_time_accepted,
IFNULL(SUM(
u1.id IS NOT NULL
AND i1.id IS NULL
AND DATE(i.created_at) = #today_date
), 0) AS today_accepted,
IFNULL(SUM(
u1.id IS NOT NULL
AND i1.id IS NULL
AND DATE(i.created_at) = #today_date - INTERVAL 1 DAY
), 0) AS yesterday_accepted
FROM
(SELECT #today_date := CURDATE()) AS var,
`user` u
LEFT JOIN `invite` i
ON i.invited_by = u.id
LEFT JOIN `user` u1
ON u1.number = i.invited_to_number
LEFT JOIN `invite` i1
ON i1.invited_to_number = i.invited_to_number
AND i1.created_at < i.created_at
GROUP BY u.id
I want to retrieve table with total count from multiple table and joins
I have 4 wordpress custom database tables.
wp_tutorials
ID tut_name
1 php
2 mysql
3 wordpress
wp_chapters
ID tut_id chapter_name
1 1 php1
2 1 php2
3 2 mysql1
4 2 mysql2
wp_series
ID chapter_id series_name
1 1 php1A
2 1 php1B
3 2 php2A
4 2 php2B
5 3 mysql1A
6 3 mysql1B
7 4 mysql2A
8 4 mysql2B
wp_tut_users
ID series_id user_name
1 2 user1
2 2 user2
3 4 user3
4 6 user4
5 7 user5
from these four tables I want to retrieve by sql query following table.
1. tutorial
tut_name total_users
php 3
mysql 2
wordpress 0
expecting best ways...
Use a left join to get even tutorials with no users
select t.tut_name, count(u.id) as total_users
from wp_tutorials t
left join wp_chapters c on c.tut_id = t.id
left join wp_series s on s.chapter_id = c.id
left join wp_tut_users u on u.series_id = s.id
group by t.tut_name
I have join with a multiple tables. You can find the tables and MySQL query in this demo.
These are the tables:
users
id name
1 abc
2 xyz
3 pqr
friend
user_id friend_id
1 2
1 3
2 3
collection
id user_id friend_id amount
1 1 2 100
2 2 1 -100
3 2 3 200
4 3 2 -200
5 1 3 300
6 3 1 -300
bill
id use_id(bill_creator)
1 1
2 2
3 2
bill_person
id bill_id user_id
1 1 1
2 1 2
3 1 3
4 2 2
5 2 3
6 3 2
6 3 1
So far I have made this query:
SELECT mf.id
, mf.name
, c.amount AS amount,count(bp.user_id) AS no_common_bills
FROM (
SELECT fr.user_id AS user_id
, fr.friend_id AS friend_id
FROM friend fr
JOIN users fru
ON fru.id = fr.user_id
WHERE fru.id IN (1)
UNION
SELECT fl.friend_id AS user_id
, fl.user_id AS friend_id
FROM friend fl
JOIN users flf
ON flf.id = fl.friend_id
WHERE flf.id IN (1)
) f
JOIN users mf
ON mf.id = f.friend_id
LEFT
JOIN collection c
ON c.friend_id = mf.id
AND c.user_id = f.user_id
LEFT JOIN bill_person bp
ON bp.user_id=f.user_id AND c.friend_id = mf.id
GROUP BY mf.id
ORDER BY mf.id
and my output of this query is
id NAME AMOUNT NO_COMMON_BILLS
2 XYZ 100 2
3 PQR 300 2
but I want this result:
id NAME AMOUNT NO_COMMON_BILLS
2 XYZ 100 2
3 PQR 300 1
I am getting wrong output at NO_COMMON_BILLS. Other than that, all values are correct.
Third time's a charm...
http://sqlfiddle.com/#!2/338dd/12
SELECT u.name friend
, COUNT(bp2.user_id) no_common_bills
FROM users u
LEFT
JOIN
( SELECT user_id me, friend_id them FROM friend WHERE user_id = 1
UNION
SELECT friend_id,user_id FROM friend WHERE friend_id = 1
) x
ON x.them = u.id
LEFT
JOIN bill_person bp1
ON bp1.user_id = x.them
LEFT
JOIN bill_person bp2
ON bp2.bill_id = bp1.bill_id
AND bp2.user_id = x.me
WHERE u.id <> 1
GROUP
BY friend;
FRIEND NO_COMMON_BILLS
jkl 0
mno 0
pqr 1
xyz 2
I need help with a sql statement.
table users:
user_id name
----------------------
1 joe
2 sam
5 tammy
6 jade
10 tony
table a1:
id user_id year approved
----------------------------------
1 5 2012 1
2 6 2012 0
3 6 2013 1
4 5 2013 1
5 10 2012 0
6 10 2013 0
I need to return all 5 user_id wither or not they have an entry in table a1 and I need fields: a1.id, users.user_id, users.name, a1.year, a1.approved from the join.
My attempt:
select a1.id, users.user_id, users.name, a1.year, a1.approved from users
LEFT JOIN a1 as a1 ON a1.user_id = users.user_id
Im stuck.
Thanks
I have 2 tables:
users:
id | name | club_id |
1 Bob 4
2 Jane 5
3 Alex 4
4 Paul 4
5 Tom 4
points:
user_id | club_id | amount(can vary)
1 4 10
1 2 10
2 5 10
3 4 10
3 4 10
4 4 10
3 2 10
3 4 10
I need (where users.club_id = 4 AND points.club_id = 4):
user_id | name | sum(amount)
3 Alex 30
1 Bob 10
4 Paul 10
5 Tom 0
Notice Tom is present in users but doesn't have any entries in points, so his sum should be 0. This is what throws me off in conjuction with grabbing a list from users.
Also would like this to be as efficient as possible (hence I added club_id = 4 both in users and points)
Try this:
SELECT
u.id,
u.name,
COALESCE(SUM(p.amount), 0) AS Totalpoints
FROM
(
SELECT *
FROM users
WHERE club_id = 4
) AS u
LEFT JOIN
(
SELECT *
FROM points
WHERE club_id = 4
) AS p ON p.user_id = u.id
GROUP BY u.id,
u.name;
See it in action here:
SQL Fiddle Demo
try this query
select u.id, u.name, sum(if (p.amount is null, 0, p.amount)) as totalPoint
from
user u
left join
(select * from point p where p.club_id = 4)p
on u.id = p.user_id
where u.club_id=4
group by u.id
SQL FIDDLE: