Multiple table join query - mysql

I have one master table as "master_tbl" which has following fields :
m_id(PK)
m_name
Two slave tables which can be :
Slave-1 :
---------
sl1_id PK
sl1_name
sl_m_id FK
Slave-2 :
---------
sl2_id PK
sl2_name
sl2_m_id FK
I need output as in one query like the matching records details should be displayed like :
m_id m_name sl1_name(or sl2_name)
last displaying field should be take name of matching records from slave1 or Slave2 table.

Inner Join query Use
SELECT mt.name, s1.sl1_name, s2.sl2_name FROM master_tbl as mt
INNER JOIN Slave-1 as s1 ON s1.sl_m_id = mt.m_id
INNER JOIN Slave-2 as s2 ON s2.sl2_m_id = mt.m_id

SELECT m_id, m_name, sl1_name as `sl1_name(or sl2_name)` FROM slave_1 S1
INNER JOIN master_tbl M ON S1.sl1_m_id = M.m_id
UNION
SELECT m_id, m_name, sl2_name as `sl1_name(or sl2_name)` FROM slave_2 S2
INNER JOIN master_tbl M ON S2.sl2_m_id = M.m_id

Related

How to do an udapte with subquery and groupby?

I have 2 tables, my first table named "popa" contains departments, my second table "stat" contains statistics, if I do an inner join of the 2 I get good results that I would like to put in a column "of" my table "popa".
SELECT dep, sum(i_d) FROM popa INNER JOIN stat on popa.num_dep = stat.dep group BY dep
I tried something like
UPDATE popa
SET pourcentagedeces = s from
(SELECT dep as d, sum(i_d)as s
FROM popa INNER JOIN stat on popa.num_dep = stat.dep group BY d)
WHERE dep = d
in fact i'm a little lost
as you can see i tried wht you said, but it didn't work
enter image description here
Thank you stef
You must use multiple-table UPDATE:
UPDATE popa
JOIN (SELECT dep num_dep, SUM(i_d)as s
FROM stat
GROUP BY num_dep) stats USING (num_dep)
SET popa.pourcentagedeces = stats.s;
As pointed out in the comments this is redundant storage of the aggregates but -
UPDATE `popa`
INNER JOIN (
SELECT `dep`, SUM(`i_d`) AS `s`
FROM `stat`
GROUP BY `dep`
)AS `agg` ON `popa`.`num_dep` = `agg`.`dep`
SET `pourcentagedeces` = `agg`.`s`;
Is this what you need?
You use UPDATE JOIN where one p1 is the table and p2 is the subquery, joined by the condition.
UPDATE popa p1 JOIN ( SELECT dep, sum(incid_dchosp) as s FROM popa INNER JOIN stat on popa.num_dep = stat.dep group BY dep ) p2 ON p1.dep = p2.dep
SET p1.pourcentagedeces = p2.s

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

How to get the columns from three different tables?

I have three different table as mention below
mRNA_GO
MSU7_LOC
GO_ID
Gene Ontology
GO_ID (Primary Key)
Category
Term
Evidence
miRNA_mRNA
miRNA_ID
MSU7_LOC
Table 1 is connected with table 2 by GO_ID and with table 3 by MSU7_LOC.
I want following columns in my output.
table1.MSU7_LOC
table2.GO_ID
table2.Category
table2.term
table2.Evidence
tabke3.miRNA_ID
I have written two diff query
Query 1
select gene_ontology.go_id , gene_ontology.category, gene_ontology.evidence, gene_ontology.term , mrna_go.MSU7_LOC
from gene_ontology inner join mrna_go on mrna_go.go_id = gene_ontology.go_id
where mrna_go.go_id in ('GO:0009058') ;
Which will give me following columns
table1.MSU7_LOC
table2.GO_ID
table2.Category
table2.term
table2.Evidenc
Query 2
SELECT mrna_go.go_id, mirna_mrna.mirna
from mirna_mrna inner join mrna_go on mrna_go.MSU7_LOC = mirna_mrna.MSU7_LOC
where mrna_go.go_id in ('GO:0009058') ;
which will give me
table2.GO_ID
tabke3.miRNA_ID
Can any one tell me how can I get the output using only one query not two different query..
Just join third table
SELECT
mg.go_id,
mm.mirna ,
g.go_id ,
g.category,
g.evidence,
g.term ,
mg.MSU7_LOC
FROM mirna_mrna mm
inner join mrna_go mg on mg.MSU7_LOC = mm.MSU7_LOC
inner join gene_ontology g on mg.go_id = g.go_id
where mg.go_id in ('GO:0009058') ;
just add a 2nd join in like;
SELECT gene_ontology.go_id , gene_ontology.category, gene_ontology.evidence, gene_ontology.term , mrna_go.MSU7_LOC, mrna_go.go_id, mirna_mrna.mirna
FROM gene_ontology
INNER JOIN mrna_go ON mrna_go.go_id = gene_ontology.go_id
INNER JOIN mirna_mrna ON mrna_go.MSU7_LOC = mirna_mrna.MSU7_LOC
WHERE mrna_go.go_id IN ('GO:0009058') ;

How to get source and destination city name using inner query in mysql

table_cities - city_id, city_name
table_booking - booking_id, source_city_id, destination_city_id
I want to get Booking_id | source_city_name | destination_city_name as result.
I am trying this:
SELECT * FROM table_bookings
INNER JOIN table_cities
ON table_bookings.source_city_id = table_cities.city_id
INNER JOIN table_cities
ON table_bookings.destination_city_id = table_cities.city_id;
But its giving Not unique table/alias: 'table_cities'
can someone help?
You need to add aliases to your query, this is because you join on the table table_bookings twice and select * the table names are ambiguous, so you need to add aliases after your joins to make it clear:
SELECT
table_bookings.Booking_id,
sourceCities.source_city_name,
destinationCities.destination_city_name
FROM table_bookings
INNER JOIN table_cities AS sourceCities
ON table_bookings.source_city_id = table_cities.city_id
INNER JOIN table_cities AS destinationCities
ON table_bookings.destination_city_id = table_cities.city_id;

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?