MySQL Query - Get total of sum from three different tables - mysql

I have a table sRecords with some data and three different tables with multiple rows against each sRecordID (PK) and a fees column,
the question is
How could I get the sum of total fees from all three tables matching with sRecordID?
the other three tables are cFees, oFees and iFees having fees and sRecordID (FK sRecords.sRecordID)
EDIT
I already have sRecordID so I don't think any JOIN will going to work here

select t.sRecordID,sum(t1.Fees)+sum(t2.Fees)+sum(t3.Fees) `Sum Fees`
from sRecords t
left join cFees t1 on t1.feeid=t.sRecordID
left join oFees t2 on t2.feeid=t.sRecordID
left join iFees t3 on t3.feeid=t.sRecordID
group by t.sRecordID

SELECT
sRecords.sRecordID,
SUM(cFees.fees) + SUM(oFees.fees) + SUM(iFees.fees) as total_fees
FROM sRecords
LEFT JOIN cFees ON sRecords.sRecordID = cfees.sRecordID
LEFT JOIN iFees ON sRecords.sRecordID = ifees.sRecordID
LEFT JOIN oFees ON sRecords.sRecordID = ofees.sRecordID
GROUP BY sRecords.sRecordID

Related

MySql query Get all rows from left table and common from right table

Table 1 - customer_kitchen_service_plans and data https://prnt.sc/00_ip7uWiQuq
Table 2 - kitchen_service_plans_linking and data https://prnt.sc/e_GW64THTCFK
Above two are the tables and i want to join in such a way that it should return All the rows from Table 1 and Common rows from Table 2
Join using column kitchen_service_plan_id from Table 1 and kitchen_service_plan_parent_id from Table 2
Current query is as below
select * from `customer_kitchen_service_plans` as `cksp`
left join `kitchen_service_plans_linking` as `kspl` on
`kspl`.`kitchen_service_plan_parent_id` =
`cksp`.`kitchen_service_plan_id`
where `cksp`.`status` = 'ACTIVE' and `cksp`.`customer_id` = 2
You want a left outer join which returns all rows from the first table and matching rows from the right.
select * from `customer_kitchen_service_plans` as cksp
left outer join `kitchen_service_plans_linking` as kspl on
kspl.`kitchen_service_plan_parent_id` =
cksp.`kitchen_service_plan_id`
where cksp.`status` = 'ACTIVE' and cksp.`customer_id` = 2
Here's a discussion on Left Outer Join in MySQL
See if that's help
SELECT * FROM customer_kitchen_service_plans
LEFT JOIN kitchen_service_plans_linking ON
customer_kitchen_service_plans.kitchen_service_plan_id=
kitchen_service_plans_linking.kitchen_service_plan_parent_id;

MySQL INNER JOIN Multiple columns

I have two tables
Table1: violations
Columns: date, time, pdid, pname, v1, v2, v3, v4
Each v1 through v4 has an integer value which corresponds to a single entry (ID) in table 2.
table2: parking_violations
columns: code, section, description, ID
I need to query each record of violations based on pdid, and match each 'v1-v4' to column 'ID' in the p_violations table.
SELECT
parking.date,
parking.time,
parking.pname,
parking_violations.code,
parking_violations.section,
parking_violations.description
FROM
parking
INNER JOIN parking_violations ON parking.v1=parking_violations.ID
WHERE
pdid=5
This returns the correct records for V1, but I cannot figure out how to also return V2-V4 all populated by matching the value to ID.
Like #fa06 explain, you can use multiple joins to the same table, but instead of inner join i will use left join, this way i have the flexibility of get rows where not all vN have matching IDs on the table parking_violations.
SELECT
parking.date,
parking.time,
parking.pname,
pv1.code,
pv1.section,
pv1.description,
pv2.code,
pv2.section,
pv2.description,
pv3.code,
pv3.section,
pv3.description,
pv4.code,
pv4.section,
pv4.description
FROM
parking
LEFT JOIN
parking_violations AS pv1 ON pv1.ID = parking.v1
LEFT JOIN
parking_violations AS pv2 ON pv2.ID = parking.v2
LEFT JOIN
parking_violations AS pv3 ON pv3.ID = parking.v3
LEFT JOIN
parking_violations AS pv4 ON pv4.ID = parking.v4
WHERE
parking.pdid = 5;
use join multiple time with alias like below:
SELECT
parking.date,
parking.time,
parking.pname,
parking_violations.code,
parking_violations.section,
parking_violations.description
FROM
parking
INNER JOIN parking_violations ON parking.v1=parking_violations.ID
inner join parking_violations a ON parking.v2=a.ID
inner join parking_violations b ON parking.v3=b.ID
inner join parking_violations c ON parking.v4=d.ID
WHERE
pdid=5

How to handle SQL joins if missing rows on particular table exist

SELECT
fromData.name as fromname, toData.name as toName, prodData.prodname,
t1.`from_id`, t1.`to_id` , t1.`product_id` , t1.`title`, t1.`message`, t1.`senttime` , t1.`readstatus`, t1.`responded`, t1.`merchanthidden`
FROM `inquiries` as t1
INNER JOIN users as fromData on t1.from_id = fromData.id
INNER JOIN users as toData on t1.to_id = toData.id
INNER JOIN products as prodData on t1.product_id = prodData.id
WHERE t1.id=13
Above query joins 3 tables (inquiries, users, products) together and gets data from each table.
Sometimes it is possible that items in the 'products' table get deleted. Trying to join products table by a deleted product id will fail the query.
Is there a way that I can assign 'prodData.prodname' a default value and execute query without failing in case of a missing item in products table ?
Why don't use left join insted of inner join ,
The LEFT JOIN keyword returns all records from the left table (table1), and the matched records from the right table (table2). The result is NULL from the right side, if there is no match.
SELECT
fromData.name as fromname, toData.name as toName, prodData.prodname,
t1.`from_id`, t1.`to_id` , t1.`product_id` , t1.`title`, t1.`message`, t1.`senttime` , t1.`readstatus`, t1.`responded`, t1.`merchanthidden`
FROM `inquiries` as t1
INNER JOIN users as fromData on t1.from_id = fromData.id
INNER JOIN users as toData on t1.to_id = toData.id
LEFT JOIN products as prodData on t1.product_id = prodData.id
WHERE t1.id=13

Getting NULL in a SQL left JOIN as result

create or replace view vw_fill_thresholds as
SELECT
fill_thresholds.id,
fill_thresholds.tenant_id,
waypoint_locations.name,
material_type.description as material,
fill_thresholds.can_size,
fill_thresholds.threshold,
units.description as unit,
fill_thresholds.post_threshold_cost as ptc,
ptu_units.description as ptu_unit
FROM fill_thresholds
left join fill_locations on fill_thresholds.fill_id = fill_locations.id
and fill_thresholds.deleted = 0
left join units on fill_locations.unit_id = units.unit_id
left join waypoint_locations on `waypoint_locations`.`id` = `fill_locations`.`waypoint_id`
left join `material_type` on `material_type`.`id` = `fill_locations`.`material`
left join `units` `ptu_units` on fill_thresholds.post_threshold_unit_id = units.unit_id
I need to select the same column as units. So, In my fill_thresholds table I have two fields that contain an id from the units table: example 2 and 3.
Now, in my units table I have the following fields: id and description. example data: 1 km, 2 miles, 3 yards ,etc. so in my view according to the numbers I have in fill_thresholds it should show miles and yards. Right now I'm getting NULLs which I don't understand why.
Your last join should be as per below-
left join `units` `ptu_units` on fill_thresholds.post_threshold_unit_id = ptu_units.unit_id
Right now you are joining your units table based on 2 columns first with fill_locations.unit_id = units.unit_id and 2nd with fill_thresholds.post_threshold_unit_id = units.unit_id, so not getting corresponding rows ans showing null.
The problem with your query is that you're trying to guess what is wrong analysing the whole query. It is too complicated so try to join your tables one by one.
1)fill_thresholds LEFT JOIN fill_locations
2)fill_thresholds LEFT JOIN fill_locations LEFT JOIN units
3)fill_thresholds LEFT JOIN fill_locations LEFT JOIN units LEFT JOIN waypoint_locations
and so on
This way you can find where is the problem

mysql left join with multiple tables,having only primary key as foreign key of first table

TB 1 : user_profile as ur-> id(PK),name (total 4k records all unique)
TB 2 : user_course_rel as ucr-> id,course_id,year_id,division,user_id(Fk)
TB 3 : students_attendance_lect as sal-> id,subject_id,date,student_id(Fk)
student_id(Fk) = user_id(Fk) = id(PK).
I want to left join on TB1 and get name of all students belonging to particular course,year,division and both attendees of subject and date and not attendees which should be 132 unique records.
After running following query i am getting total (4k records)
select distinct(ur.id), ur.fname
from user_profile as ur
inner join user_course_rel as ucr
on ucr.user_id=ur.id
left join students_attendance_lect as sal
on sal.student_id=ucr.user_id
and ucr.course_id=1
and ucr.year_id=1
and ucr.division=3
and sal.subject_id=2
and sal.date='2013-01-21'
Several items in your LEFT JOIN look like they should be in a WHERE clause. I'm not 100% clear what your question is but try:
select distinct(ur.id), ur.fname
from user_profile as ur
inner join user_course_rel as ucr
on ucr.user_id=ur.id
left join
(SELECT sal.student_id, sal.subject_id, sal.date
FROM students_attendance_lect as sal
WHERE sal.date='2013-01-21'
AND sal.subject_id = 2) AS sa
ON sa.student_id=ucr.user_id
WHERE ucr.course_id=1
and ucr.year_id=1
and ucr.division=3
The way you had written it was asking the DB to LEFT JOIN on any row that had a course id of 1, a division of 3, a subject id of 2 or a date of '2013-01-21', do you see?