Table 1: Building
ProjectNO (FK)
BuildingNO
Floors
location
Table 2: Project
ProjectNO (PK)
ProjectName
CityName
I need to join "project" and "Building" because I need the common Buildings in Project and Building by the key ProjectNO.
thank you
you should use inner join keyword to get matching records from both tables.
If you have 2 tables with the same ProjectNO to be joined,
select * from Project p
inner join
Building b
on p.ProjectNO = b.ProjectNO;
You seems want :
select b.*
from Building b
where exists (select 1 from Project p where p.ProjectNO = b.ProjectNO);
If you want ProjectName, CityName then you can do JOIN :
select p.*, b.*
from Project p inner join
Building b
on p.ProjectNO = b.ProjectNO;
you should do inner join to get matching record from both tables like follows...
SELECT * FROM Building INNER JOIN Project ON Project.ProjectNO=Building.ProjectNO;
it will return only those records whose ProjectNO exists in both tables.
just use inner join because The INNER JOIN keyword selects records that have matching values in both tables.
select p.*, b.ProjectName
from Project p inner join
Building b
on p.ProjectNO = b.ProjectNO;
Related
I have 2 tables, Products and Orders. If somebody creates an order, I want Product_Name01 from Orders to be filled in by the Product_Name from Products.
enter image description here
The code below does not work. What am I doing wrong?
SELECT * FROM Sandbox.dbo.orders o
SELECT * FROM Sandbox.dbo.Products p
INNER JOIN o -- is this the target table?
WHERE EXISTS (
SELECT Product_Name01
FROM o
WHERE b.Product_ID01 = o.Product_ID )
I think the inner join already "checks" if both tables have matching records.
"The INNER JOIN keyword selects records that have matching values in both tables."- - https://www.w3schools.com/sql/sql_join_inner.asp
So your query should probably just look like this:
SELECT * FROM Sandbox.dbo.orders o
INNER JOIN Sandbox.dbo.Products p
ON b.Product_ID01 = o.Product_ID;
I want to return the data of all the tables linked together via foreign keys when the tables are linked together in a chain. Is there a simple SQL command for this?
SAMPLE DATA: A entrance has floor_id fk to floors table, floor table has a building_id fk to buildings, buildings has a company_id fk to companies.
Can I get all the above data given just the entrance id?
Is there a more elegant way to do this then the following SQL:
SELECT * FROM floor_entrance
LEFT JOIN floor ON floor.id = (SELECT floor_id FROM floor_entrance WHERE id = {floor_entrance_id})
LEFT JOIN building ON building.id = (SELECT building_id FROM floor WHERE id =
(SELECT floor_id FROM floor_entrance WHERE id = {floor_entrance_id}))
LEFT JOIN company ON company.id = (SELECT company_id FROM building WHERE id =
(SELECT building_id FROM floor WHERE id =
(SELECT floor_id FROM floor_entrance WHERE id = {floor_entrance_id})))
WHERE floor_entrance.id = {floor_entrance_id}
I am looking to achieve a concise way to write this postgreSQL command.
DESIRED RESULTS: "LEFT JOIN ALL ON FOREIGN KEYS ACROSS THE WHOLE SCHEMA"?
I would simply do it like this
SELECT * FROM floor_entrance A
LEFT JOIN floor B ON A.floor_id = B.id
LEFT JOIN building C ON B.building_id = C.id
LEFT JOIN company D ON C.company_id = D.id
WHERE A.id = {floor_entrance_id}
There appears to be no one simple way to do this. However the answer by #Alexis.Rolland is a neat way to do this even though you must left join each table manually.
I 'd like to check if i'm using the best method about such a case:
2 Tables:
First table : information about invoice, with notably 2 columns :
=> ID responsible 1 (columns id_user1) and ID responsable 2 (columns id_user2)
Table : tb_invoice
Second Table : id, name of all responsables.
Table : tb_users (columns : id_user, name ...)
And for getting information about the 2 responsables on the invoice, I do the link like this :
select
t1.*,
tu1.name as responsable_1,
tu2.name as responsable_2
from
tb_invoice t1
inner join tb_users tu1 on tu1.id_user=t1.id_user1
inner join tb_users tu2 on tu2.id_user=t1.id_user2
where
t1.num = 123456
Is there another solution and better ?
Thanks for your help.
Regards,
I fixed your aliases confusion (edited your question) and your query is a pretty good solution
select
t1.id,
tu1.name as responsable_1,
tu2.name as responsable_2
from
tb_invoice t1
inner join tb_users tu1 on tu1.id_user=t1.id_user1
inner join tb_users tu2 on tu2.id_user=t1.id_user2
where
t1.num = 123456
If one invoice can have only one (or no) responsable so it is recommended to use Left Join instead of Inner Join
I want to select all rows of the table PERSON that has no records into DOCUMENT table that have the primary table as FK.
What could be the better for me? LEFT JOIN? NOT IN? Any other solution?
Here's the simple scheme:
PERSON:
personId
personName
personSex
DOCUMENT:
documentId
FK_Person
Thank you in advance
Use a left join and check if the link to the other table failed (is null)
select p.*
from person p
left join document d on p.personId = d.fk_person
where d.fk_person is null
See this explanation of joins
try using not exist or left join :
select P.*
from PERSON P left join DOCUMENT D on P.personId = D.FK_Person
where D.FK_Person is null
OR
select * from PERSON P
where not exists (select 1 from DOCUMENT D where P.personId = D.FK_Person)
I have Visual Studio 2005 and am writing code in VB, not C+. I need a Select statement and have found some that are close but not for my situation. I have three tables:
PROJECT
[Projnum]
[ShipDate]
CUSTOMER
[Projnum]
[Jobnum]
TAGS
[Jobnum]
I need to join CUSTOMER and PROJECT so I know all the CUSTOMER.Jobnum records where PROJECT.ShipDate is null. Out of those records, I then need to get which ones do not have a match from TAGS.Jobnum.
Any help would be much appreciated. Thanks a bunch, Chuck.
SELECT c.Jobnum
FROM customer c
INNER JOIN project p
ON c.Projnum = p.Projnum
WHERE p.ShipDate IS NULL
AND NOT EXISTS(SELECT NULL FROM tags t WHERE t.Jobnum = c.Jobnum)
If I understand correctly:
PROJECT
ProjNum
ShipDate
CUSTOMER
ProjNum
JobNum
TAGS
JobNum
And you want all unshipped projects:
SELECT c.JobNUm
FROM Project p
INNER JOIN Customer c
ON c.ProjNum = p.ProjNum
WHERE p.ShipDate is null
And then you want the ones without a JobNum in the TAGS table:
SELECT c.JobNUm
FROM Project p
INNER JOIN Customer c
ON c.ProjNum = p.ProjNum
WHERE p.ShipDate is null
AND c.JobNum NOT IN (SELECT JobNum from TAGS)
Could be done simpler, but I wanted to show my work.
A LEFT JOIN may be faster than NOT IN and NOT EXISTS.
SELECT c.JobNUm
FROM Project p
INNER JOIN Customer c
ON c.ProjNum = p.ProjNum
LEFT JOIN tags t ON t.jobnum=c.jobnum
WHERE p.ShipDate is null AND t.jobnum IS NULL