I have three tables in sql-server like table A table B table C.
How can I join 3 tables as expressed in the image below?
More information needed to give you a correct piece of code, but from the image you need LEFT JOINs.
(ID's have been presumed)
SELECT *
FROM Customers c
LEFT JOIN Items i ON c.iid = i.id
LEFT JOIN Sales s ON c.sid = s.id
It's never too late :)
Most probably you'll need this:
select ...
from customers
full outer join (items inner join sales on (xxx)) on (xxx)
Related
Database design here
I want to get records from the product_spec_data table that are associated with products whose category_id is 5.
Please help me make a query to the database...
All of the relationship are clearly laid out, a simple join across tables would works.
SELECT psd.*
FROM product_spec_data psd
INNER JOIN product_spec_values psv ON psd.id = psv.product_spec_data_id
INNER JOIN products prod ON psv.product_id = prod.id
INNER JOIN categories cat ON prod.category_id = cat.id
-- with category id = 5
WHERE cat.id = 5;
I am not sure what do you mean by complex query. I guess your question is not complete. I think the query for your question (if it is T-SQL) will be as follows
select d.*
from product_spec_data d
left outer join product_spec_values v on d.id=v.product_spec_data_id
left outer join products p on v.product_id=p.id
where p.category_id=5
(I think in MySQL also above syntax will remain same, you may remove [outer] clause in MySQL)
I have two tables, "Booking" and "City". CityName field is primary key in City table and I have used it as foreign key for two columns "SourceCity" and "DestinationCity" in Booking table. I want to create a stored procedure to select all existing data from the Booking table for creating a view list, for which I have written the following.
SELECT [dbo].[Booking].[BookingID],
[dbo].[Booking].[CustomerName],
[dbo].[City].[CityName],
[dbo].[City].[CityName],
[dbo].[Booking].[StartingDate],
[dbo].[Booking].[EndingDate],
[dbo].[Car].[LicensePlateNumber],
[dbo].[Driver].[DriverName],
[dbo].[Booking].[AdvanceTaken],
[dbo].[Booking].[PendingPayment],
[dbo].[Booking].[TotalRent],
[dbo].[Booking].[BookingDate],
[dbo].[Booking].[IDProof]
FROM [dbo].[Booking]
**LEFT OUTER JOIN [dbo].[City]
ON [dbo].[Booking].[SourceCity] = [dbo].[City].[CityName]
AND [dbo].[Booking].[DestinationCity] = [dbo].[City].[CityName]**
LEFT OUTER JOIN [dbo].[Driver]
ON [dbo].[Driver].[DriverID] = [dbo].[Booking].[DriverAllotted]
LEFT OUTER JOIN [dbo].[Car]
ON [dbo].[Car].[CarID] = [dbo].[Booking].[CarAllotted]
ORDER BY [dbo].[Booking].[BookingID]
I am not sure if it is possible to do the following
LEFT OUTER JOIN [dbo].[City]
ON [dbo].[Booking].[SourceCity] = [dbo].[City].[CityName]
AND [dbo].[Booking].[DestinationCity] = [dbo].[City].[CityName]
I guess you need a different JOIN
FROM [dbo].[Booking] as booking
LEFT OUTER JOIN [dbo].[City] as source_city
ON booking.[SourceCity] = source_city.[CityName]
LEFT OUTER JOIN [dbo].[City] as destination_city
ON booking.[DestinationCity] = destination_city.[CityName]
....
Yes it is possible, you just need to use a different table alias. Beyond referencing the same table twice, table aliases can make your code look a lot cleaner, e.g.
SELECT b.CustomerName,
sc.CityName AS SourceCity,
dc.CityName AS DestinationCity,
b.StartingDate,
b.EndingDate,
c.LicensePlateNumber,
d.DriverName,
b.AdvanceTaken,
b.PendingPayment,
b.TotalRent,
b.BookingDate,
b.IDProof
FROM dbo.Booking AS b
LEFT OUTER JOIN dbo.City AS sc
ON sc.CityName= b.SourceCity
LEFT OUTER JOIN dbo.City AS dc -- Different Alias here
ON dc.CityName = b.DestinationCity
LEFT OUTER JOIN dbo.Driver AS d
ON d.DriverID = b.DriverAllotted
LEFT OUTER JOIN dbo.Car AS c
ON c.CarID = b.CarAllotted
ORDER BY
b.BookingID;
I appreciate that cleaner is somewhat subjective, but I would be astonished if anyone found this harder to read than your original query
I have the following DB structure:
And right now I can't make up a query to get
a creator data, admin data and tech data from item_contacts...
What kind of JOIN I need to use and how?
I think you want 3 joins on item_contacts - one for each column whose data you want to recover:
select
i.*,
cc.data as creator_data,
ca.data as admin_data,
ct.data as tech_data
from items i
inner join item_contacts cc on cc.contact_id = i.creator_id
inner join item_contacts ca on ca.contact_id = i.admin_id
inner join item_contacts ct on ct.contact_id = i.tech_id
Sorry for Title, don't know how to explain.
Ok so I want to see if any protocol (PTC_ID) is linked to an Audit (AUD_ID), in the picture you can see there is 3 tables and each one has a value of the other.
I though of using inner join all 3 tables with the ON , ON ADA_PTCID = PTC_ID etc. and if a audit is linked with a PTC then display year?
Select AUD_YEAR
From AUD_Table at
Inner Join ADA_TABLE ad
ON at.AUD_ID = ad.ADA_AUD_ID
Inner Join PTC_TABLE pt
ON pt.PTC_ID=ad.ADA_PTCID
try
select
ptc.ptc_name,
aud.aud_year
from
ptc_table ptc
inner join
ada_table ada
on
ada.ada_ptcid=ptc.ptc_id
inner join
aud_table aud
on
aud.aud_id=ada.ada_aud_id
Something like this?
select
aud.year,ptc.name
from ada
inner join aud on ada.aud_id = aud.aud_id
inner join ptc on ada.ptc_id = ptc.ptc_id
I need to join 5 tables to get particular bill number.
Tables are
bill,
Service_bill
Damage_cost
Extraperson_cost
Advance_cost
Except bill, all other tables are may store null values.
My query is like below...
select bill.bill_no,
bill.total,
bill.discount,
bill.to_be_paid,
isnull(Service_bill.total_amt,0) as ServiceCharge,
isnull(Damage_cost.total_amt,0) as DamageCost,
isnull(Extraperson_cost.total_amt,0) as ExtraCost,
isnull(Advance_cost.total_amount,0) as Advance
from bill
left join Advance_cost on bill.bill_no=Advance_cost.room_bill_no and bill.bill_no='57'
inner join Service_bill on bill.bill_no=Service_bill.room_bill_no
inner join Damage_cost on bill.bill_no=Damage_cost.room_bill_no
inner join Extraperson_cost on bill.bill_no=Extraperson_cost.room_bill_no
Now it returns data's, which joins conditions are getting true.
First table should have values then other tables are null only so it must be return first tables completely. But i don't know why it comes like this!
The inner joins mean that you are only finding bills for people who have records in all 4 other tables. Use outer joins instead.
Also, you are limiting the results to those items where there is a record in "advance_cost" for bill 57. That's probaly not the idea...
Thanks Neville k... i got it using outer join...
select bill.bill_no,bill.total,bill.discount,bill.to_be_paid,
isnull(Service_bill.total_amt,0) as ServiceCharge,
isnull(Damage_cost.total_amt,0) as DamageCost,
isnull(Extraperson_cost.total_amt,0) as ExtraCost,
isnull(Advance_cost.total_amount,0) as Advance
from bill
left join Advance_cost on bill.bill_no=Advance_cost.room_bill_no
left outer join Service_bill on bill.bill_no=Service_bill.room_bill_no
left outer join Damage_cost on bill.bill_no=Damage_cost.room_bill_no
left outer join Extraperson_cost on bill.bill_no=Extraperson_cost.room_bill_no
where bill.bill_no='57'