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
Related
I have two tables as follows:
docs
id
carid
name
1
1
doc1
2
1
doc2
3
2
doc3
4
1
doc4
5
5
doc5
cars
carid
parentid
name
1
4
car1
2
5
car2
3
4
car3
4
4
car4
5
5
car5
Question: I want to write a query in mysql where I can pass the carid in where clause and get all the rows from docs table where the parentid is same as that of the passed carid.
Desired Outcome If I pass carid=3 then the rows 1,2,4 from docs table should be returned as the parentid is 4 for carids 1,3,4.
Simillarly, If I pass carid=2 then the rows 3,5 from docs table should be returned as the parentid is 5 for carids 2.5.
You need to join the cars-table twice. First for the condition and second for the parent:
select d.*
from cars c
join cars p on p.parentid=c.parentid
join docs d on d.carid=p.carid
where c.carid=3
You're thinking about this a little wrong in the aspect of a relational database .. You SHOULD have 4 tables:
docs
doc_id
name
1
doc1
2
doc2
3
doc3
4
doc4
5
doc5
cars
car_id
name
1
car1
2
car2
3
car3
4
car4
5
car5
cars_to_docs
car_id
doc_id
1
1
1
2
1
4
2
3
5
5
parents_to_car
car_id
parent_id
1
4
2
5
3
4
4
4
5
5
Then you could simply use a basic JOIN
SELECT b.doc_id FROM test.docs a
LEFT JOIN test.cars_to_docs b
ON a.doc_id = b.car_id
LEFT JOIN test.parents_to_car c
ON c.car_id = b.car_id
LEFT JOIN test.cars d
ON c.car_id = d.car_id
WHERE c.parent_id = (SELECT parent_id FROM test.parents_to_car WHERE car_id = 3)
This will give you your output of 1,2,4
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
I am stuck in query I have a table like this
budget_details
id budget_id expenditure_head_id budget
1 1 1 1233
2 1 2 333
3 1 3 567
4 2 1 343
5 2 2 343
6 2 3 6767
7 2 4 557
expenditure_heads
id name
1 abc
2 xyz
3 qwe
4 uvw
I want to get all the expenditure_heads from budget_details that even
if not in budget_details like here budget_id=1 does not contain expenditure_head_id 4
but I want to select that to with 0 or null displaying
I tried this but it not displaying expenditure_head_id 4
select `expenditure_heads`.`name`, `budget_details`.`budget`, `budget_details`.`id` from
`budget_details`
right join `expenditure_heads` on `budget_details`.`expenditure_head_id` = `expenditure_heads`.`id`
where `budget_details`.`budget_id` = 1"
The where avoid you to get the missing row you need. The left join is done on the ON statement, so this query should work for you:
SELECT EH.name, BD.budget, BD.id FROM expenditure_heads EH
LEFT JOIN budget_details BD
ON (BD.expenditure_head_id = EH.id AND BD.budget_id = 1)
I have this table:
ID STUDENT CLASS QUESTION ANSWER TIME
1 1 1 1 c 12:30
2 1 1 1 d 12:36
3 1 1 2 a 12:38
4 2 1 1 b 11:24
5 2 1 1 c 11:26
6 2 1 3 d 11:35
7 2 3 3 b 11:24
I'm trying to write a query that does this:
For each STUDENT in a specific CLASS select the most recent ANSWER for each QUESTION.
So, choosing class "1" would return:
ID STUDENT CLASS QUESTION ANSWER TIME
2 1 1 1 d 12:36
3 1 1 2 a 12:38
5 2 1 1 c 11:26
6 2 1 3 d 11:35
I've tried various combinations of subqueries, joins, and grouping, but nothing is working. Any ideas?
You can use a sub-query to get most recent ANSWER per QUESTION, Then use this as a derived table and join back to the original table:
SELECT m.*
FROM mytable AS m
INNER JOIN (
SELECT STUDENT, QUESTION, MAX(`TIME`) AS mTime
FROM mytable
WHERE CLASS = 1
GROUP BY STUDENT, QUESTION
) AS d ON m.STUDENT = d.STUDENT AND m.QUESTION = d.QUESTION AND m.`TIME` = d.mTime
WHERE m.CLASS = 1
Demo here
I have 3 tables flowing and i try to build a query to show the results. But it just show exactly half.
Project
--------------
id name
--------------
1 Project 1
2 Project 2
3 Project 3
4 Project 4
5 Project 5
6 Project 6
Pj_rp
-----------------------------
id id_pj id_rp
-----------------------------
1 1 1
2 2 2
3 1 3
4 2 4
5 1 5
6 3 6
Report
--------------
id Fee
--------------
1 200
2 200
3 400
4 400
5 400
6 400
I want to get results
**result**
--------------------
Project SUM(Fee)
--------------------
Project 1 1000
Project 2 600
Project 3 400
Project 4 NULL
Project 5 NULL
Project 6 NULL
And i built the folowing query but it wrong
SELECT
a.name, c.Fee
from
Project a
LEFT JOIN
Pj_rp b ON (a.id = b.id_pj)
LEFT JOIN
Report c ON (b.id_rp = c.id)
GROUP BY a.tongmucdautuduan_usd
I have no idea, who can help me to solve this?
Thank you!
SELECT
a.name "Project", sum(c.Fee)
from
Project a
LEFT JOIN
Pj_rp b ON (a.id = b.id_pj)
LEFT JOIN
Report c ON (b.id_rp = c.id)
GROUP BY a.name