How to create a nested query with 3 tables in mysql - mysql

I have 3 tables, as shown in image.
I want a query which give me all fields from purchase_master, and package_title from package_master (purchase_master.package_id == package_master.id) + user_name from user_master (purchase_master.user_id == user_master.user_id).
I could not figure out how to join 3 tables.
Need some help.

The general idea would be:
SELECT pum.*, pam.package_title, um.user_name
FROM purchase_master AS pum
LEFT JOIN package_master AS pam ON pam.package_id = pam.id
LEFT JOIN user_master AS um ON um.user_id = pum.user_id
WHERE //some condition?
Is this what you require?

Select a.package_id,a.user_id,a.package_details,b.package_title,c.user_name from purchase_master as a join paclage_master as b join user_master as c where a.package_id=b.id and a.user_id=c.user_id
Try this

Joining three tables is same as joining two tables.
You can use following query to achieve this.
SELECT P.PACKAGE_ID,P.USER_ID,P.PACKAGE_DETAILS,PM.PACKAGE_TITLE
FROM PURCHASE_MASTER P
JOIN PACKAGE_MASTER PM ON P.PACKAGE_ID= PM.ID
JOIN USER_MASTER U ON P.USER_ID=U.USER_ID

Related

Join 4 tables with specified column

I have this code:
SELECT A.UNITCODE, B.FORMATIONCODE, C.UPPERFORMATIONCODE, D.UPPERFORMATIONCODE
FROM UNIT AS A.UNITCODE
INNER JOIN FORMATION AS B.FORMATIONCODE
INNER JOIN UPPERFORMATION_UNIT AS C.UPPERFORMATION
INNER JOIN UPPERFORMATION AS D.UPPERFORMATIONCODE
WHERE UNITCODE='7000007'
Can you guys help me how to join 4 tables with specified column?
Assuiming the all 3 related tables have the same UNIT_ID for join with table UNIT
SELECT
A.UNITCODE
, B.FORMATIONCODE
, C.UPPERFORMATIONCODE
, D.UPPERFORMATIONCODE
FROM UNIT AS A
INNER JOIN FORMATION AS B ON B.FORMATIONCODE = A.UNIT_ID
INNER JOIN UPPERFORMATION_UNIT AS C. C.UPPERFORMATION = A.UNIT_ID
INNER JOIN UPPERFORMATION AS D D.UPPERFORMATIONCODE = A.UNIT_ID
WHERE UNITCODE='7000007'
It seems you are confusing table aliases and linking columns.
This is how to give a table an alias name in the query in order to enhance readability:
INNER JOIN formation AS f
where the AS is optional. Most often it is ommited.
This is how to join:
FROM unit AS u
INNER JOIN upperformation_unit AS ufu ON ufu.unitcode = u.unitcode
Well, I don't know the columns linking the tables of course, let alone their names. But I suppose the query would have to look like this more or less:
SELECT
u.unitcode,
f.formationcode,
uf.upperformationcode,
ufu.upperformationcode
FROM unit u
JOIN upperformation_unit ufu ON ufu.unitcode = u.unitcode
JOIN upperformation uf ON uf.upperformationcode = ufu.upperformationcode
JOIN formation f ON f.formationcode uf.formationcode
WHERE u.unitcode = 7000007;

MySQL how to select what I need, most likely an inner join

A bit of a newbie question, probably an INNER JOIN with an "AS" statement, but I can't figure it out...
This is for a MYSQL based competition app. I want to select the "img_title" for both img_id1 and img_id2. I can't figure out how to do it and still see which title is assigned to the associated _id1 or _id2.
My tables:
competitions
comp_id
img_id1
img_id2
on_deck
img_id
img_title
Desired results:
comp_id | img_id1 | img_title1 |img_id2 | img_title2
You need a join for each image:
SELECT comp.comp_id, img1.img_id, img1.img_title, img2.img_id, img2.img_title
FROM competitions comp
INNER JOIN on_deck img1 ON img1.img_id = comp.img_id1
INNER JOIN on_deck img2 ON img2.img_id = comp.img_id2
LEFT JOIN if img_id1 or img_id2 can be NULL.
select comp_id, img_id1, b.img_title as img_title1,
img_id2, b2.img_title as img_title2
from competitions a
left outer join on_deck b on b.img_id = a.img_id1
left outer join on_deck b2 on b2.img_id = a.img_id2
switch let outer join to inner join if you want to exclude rows in competitions that do not have two matching img_ids
This query should give you the results you want. This also assumes that comp.img_id1 and comp.img_id2 are never NULL.
SELECT comp.comp_id
, comp.img_id1
, deck1.img_title AS img_title1
, comp.img_id2
, deck2.img_title AS img_title2
FROM competitions AS comp
JOIN on_deck deck1 ON comp.img_id1 = deck1.img_id
JOIN on_deck deck2 ON comp.img_id2 = deck2.img_id
If you have plan on having a NULL or empty string comp.img_id1 and/or comp.img_id2 fields, you'll need to do some left joins.

Mysql query with multiple joins and conditions

I am trying to connect four tables using joints,here i used left join to connect tables and my condition is all the goods,item should be same and all the site should be same.same site have multiple goods, so i want to get the sum number of goods from each table. my query is given
select
a.goods
,sum(a.no_of_units) as totala
, a.site
,b.item
,sum(b.quantity) as totalb
,b.site
,c.goods
,c.site
,sum(c.no_of_units) as totalc
,d.site
,d.goods
,sum(d.quantity)b as totald
from
inward_stock a
left join
opening_balance b
on
a.site=b.site
and
a.goods=b.item
left join
return_stock c
on
b.site=c.site
and
b.item=c.goods
left join
stock_consumed d
on
d.site=c.site
and
d.goods=c.goods
Can you put your conditions at the end of the joints like this:
select a.goods,sum(a.no_of_units) as totala, a.site,b.item,sum(b.quantity) as totalb,b.site,c.goods,c.site,sum(c.no_of_units) as totalc,d.site,d.goods,sum(d.quantity) as totald
from inward_stock a left join
opening_balance b on (a.site=b.site) left join
return_stock c on (b.site=c.site) left join
stock_consumed d on (d.site=c.site) where (a.goods=b.item) and (b.item=c.goods) and (d.goods=c.goods)
I have not tested your request but it seems that it's not bad.

Join data from multiple columns and 2 tables

Those 2 are my tables I use for my data. Now when I want to join those two tables I stuck at JOIN ON golub...
I know I'm making my mistake there but I don't know what is it. Values beneath IDmajka and IDotac sometimes may be 0. That value 0 is from table "golub" and it doesn't exist. Even If I put values that exists in table "golub" it still doesn't work. It won't collect any data.
Please ignore JOIN on drzava and status cause it works.
my query
SELECT * FROM popis_golubova
JOIN golub ON (golub.ID = popis_golubova.IDgolub
AND golub.ID = popis_golubova.IDmajka
AND golub.ID = popis_golubova.IDotac)
JOIN drzava ON (drzava.ID=popis_golubova.IDdrzava)
JOIN status ON (status.ID=popis_golubova.IDstatus)
WHERE popis_golubova.IDkorisnik='$ID_KORISNIK'
table "golub"
table "popis_golubova"
This is solution if it is going to help someone
SELECT
O.brojgoluba AS o_brojgoluba,
M.brojgoluba AS m_brojgoluba,
golub.spol, golub.boja, golub.rasa, golub.ime, golub.godina, golub.brojgoluba, drzava.drzava, status.status
FROM popis_golubova
JOIN drzava ON (drzava.ID=popis_golubova.IDdrzava)
JOIN status ON (status.ID=popis_golubova.IDstatus)
JOIN golub AS O ON (O.ID=popis_golubova.IDotac)
JOIN golub AS M ON (M.ID=popis_golubova.IDmajka)
JOIN golub ON (golub.ID=popis_golubova.IDgolub)
WHERE popis_golubova.IDkorisnik='$ID_KORISNIK'
ORDER BY popis_golubova.IDgolub

MySQL - Using column value for joining in the same query

I have three tables that looks something like this:
Table joins
|ID|JOIN_NAME|
1 persons
2 companies
Table information
|ID|JOIN_ID|
1 1
2 2
Table information_extra_persons
|ID|INFORMATION_ID|NAME|
1 1 John
Table information_extra_companies
|ID|INFORMATION_ID|NAME|
1 2 IBM
How can i join together these tables in one SQL? I've tried something like:
SELECT * FROM `information`
INNER JOIN `information_extra_(SELECT `name` FROM `joins` WHERE `id` = `join_id`)`
ON `information_extra_(SELECT `name` FROM `joins` WHERE `id` = `join_id`)`.`information_id` = `information`.`id`
but I can't get it to work. Of course this isn't my actual table setup, but it's the same principle. Does anyone know how to get all the info in just one SQL?
That's actually four tables, not three. This isn't just a nitpick - it looks as though the substance of your question is "how can I use the name of the table as part of the join criteria?" (ie. how can the information_extra_ tables be treated as a single table?)
To which the answer is: you can't. (Outside of dynamic SQL.)
In this specific case, the following should return what I think you are looking for:
select j.join_name joined_entity,
case when j.join_name = 'persons' then p.name
else c.name
end joined_entity_name
from information i
inner join joins j on i.join_id = j.id
left join information_extra_persons p on i.id = p.information_id
left join information_extra_companies c on i.id = c.information_id
Alternatively, a less efficient (but more general) approach might be:
select j.join_name joined_entity,
v.name joined_entity_name
from information i
inner join joins j on i.join_id = j.id
inner join (select 'persons' entity, information_id, name from information_extra_persons
union all
select 'companies' entity, information_id, name from information_extra_companies) v
on i.id = v.information_id and j.join_name = v.entity