Select, Join left, Union. Help me, - mysql

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

Related

MySQL join based on where from 2nd table

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

MySQL filter out subset by group by

1A
a b c
1 1 6
1 1 7
2 1 8
2 2 2
2 2 9
B
a b c
1 1 7
2 2 9
I want to filter out a subset of A
a b c
1 1 6
2 2 2
I am intend to join two tables by group by column a, b
such that to select the value in column c is less than the c value in table B, which is the desired subset.
But don't know how to implement this.
Try this:
SELECT A.* FROM A INNER JOIN B
ON A.a=B.b AND A.c<B.c;
See MySQL Join Made Easy tutorial.

Select record from table with not in other table

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)

mysql count and join

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

Arranging Data according to their page number in reports

I got this problem regarding the pages the data is supposed to be placed.
This is how they look on the database.
CODE PAGE
---- ----
A 1
A 2
A 1
B 2
B 2
C 3
C 3
C 4
D 4
D 4
D 4
D 3
My desired output is
CODE PAGE
---- ----
A 1
A 1
A 1
B 2
B 2
C 3
C 3
C 3
D 4
D 4
D 4
D 4
How can I do this?
Works in SQL SERVER
select Code,Dense_Rank() Over (order by [Code]) Page from TableName
DEMO
like this:
select CODE,PAGE from <myTABLE>
order by PAGE,CODE