Complex update with three tables involved - sql

I am using Postgresql in my company as a primary store and I'm struggling to implement an update query involving three different tables.
Here is the schema :
Table 1 has entity_id, entity_type and company_id (referencing Company.id) columns.
Company has id column
Special_company has id (which corresponds to Table1.entity_id) and company_id (referencing Company.id)
I would like to update all the rows of Table 1 where Table1.entity_type = 'SpecialCompany' in order to fill Table1.company_id such that :
Table1.entity_id = SpecialCompany.id and SpecialCompany.company_id = Company.id
I've started something like that:
UPDATE Table1
SET company_id = (select c.id
FROM company c
INNER JOIN special_company w ON c.id=w.company_id
WHERE w.id=709)
WHERE entity_type='SpecialCompany' AND entity_id=709;
But I am not able to replace 709 by all the Table1.entity_id where Table1.entity_type = 'SpecialCompany'.
Any help would be much appreciated.

I assume you are looking for something like this.
update t
set t.company_id=c.id
from
Company C
INNER JOIN special_company w ON c.id=w.company_id
INNER JOIN Table1 t on t.entity_id=w.entity_id
Where t.entity_type = 'SpecialCompany'

I'm answering to my question as I found the solution :
UPDATE Table1 t
SET company_id = c.id
FROM Company c
INNER JOIN special_company w ON c.id=w.company_id
WHERE
t.entity_type = 'SpecialCompany' AND t.entity_id=w.id;
Thanks #Krishna for your help !

Related

SQL Server: update column with select result

I have table Student with a column called IdStudent.
The value of IdStudent is 0
Table Student also have a column called UID
I need to update IdStudent in table Student with IdCandidate in table Candidate.
Table Candidate also have UID column containing the same UID of table Student.
So we can do this to have IdCandidate:
select C.IdCandidate from Candidate as C inner join Student as S
on C.UID = S.UID
How can I update IdStudent in table Student with this IdCandidate obtained in this select?
Thanks!
Use JOIN in update
update s set s.IdStudent = C.IdCandidate
from Candidate as C
inner join Student as S on C.UID = S.UID
You can do it using the following query :
update S set S.IdStudent = C.IdCandidate
from Student S
inner join Candidate C on S.UID=C.UID

How do I do an SQL query based on a foreign key field?

I have the following tables:
people:
id, name
parent:
id, people_id, name
I have tried the following:
SELECT * FROM people
LEFT JOIN parent ON people.id = parent.people_id
WHERE parent.name != 'Carol';
How do I find all the people whose parent's name is not Carol?
You can try below code
select people.name from people
inner join parent on people.id=parent.people_id
where parent.name not in ('Carol')
If the two tables are to be queried by using Foreign Key.
If you want to get all records from one table that have some related entry in a second table then use Inner join
Select * from People INNER JOIN parent ON people.id = parent.people_id
WHERE parent.name <> 'Carol'
Similarly LEFT JOIN will get all records from the LEFT linked table but if you have selected some columns from the RIGHT table, if there is no related records, these columns will contain NULL
First of all, why would you need two tables? why can't you have a single table named "Person" with ID,Name,ParentID columns
Where ParentID will be optional and reference the ID if it has got parent.
And run the following query
select * from PERSON where Name not like 'Carol%' and ParentID IS NOT NULL;
SELECT * FROM people WHERE EXISTS(SELECT 1 FROM parent WHERE people_id = id AND name <> 'Carol')
First of all the table structure you have taken restrict the future growth. Like in future if you want to add parents of your parents then it wont work in this table structure.
You can do like :
id | parent_id | people_name
Here you can make parent_id null for the parent and add parent_id as id for those who have parent. Here to retrieve you have to use SELF join(join in the same table)
`
Select * from people P
INNER JOIN parent PA ON PA.people_id = P._id
where PA.name not in ('Carol')
`
Difference between INNER JOIN and LEFT OUTER JOIN
is
1) INNER JOIN bring only similar data between two table
for ex if in people table parent_id table is nullable then it will not discard the complete row,but in case of LEFT OUTER JOIN it will bring all the rows from LEFT table as well as related table from right table.with all null in right joined row..

Join 3 tables with different FKs

I'm trying to join 3 tables together in a SELECT query with some WHERE clauses.
Table 1 is linked to Table 2, and Table 2 is linked to Table 3.
The tables are as follows:
Author
PK: Author_ID
FK: Location_ID
Author_First_Name
Location
PK: Location_ID
City
Articles
PK: Article_ID
FK: Author_ID
Article_Name
So far I've put together this Query, but am having trouble determining how to to Join the second Foreign Key 'Location_ID'
SELECT
Articles.Article_Name
FROM
Articles
INNER JOIN Author
ON Articles.Author_ID
INNER JOIN Location
ON
WHERE Author.Author_First_Name='Sam'
AND Location.City<>'Detroit'
The proper syntax is something like this:
SELECT a.Article_Name
FROM Articles a INNER JOIN
Author au
ON a.Author_ID = au.Author_ID INNER JOIN
Location l
ON l.Location_ID = au.Location_Id
WHERE au.First_Name = 'Sam' AND l.City <> 'Detroit';
Note the use of table aliases make the query easier to write and to read. Also, you need conditions connecting the two tables for the ON clauses.
You forgot to compare table fields
SELECT * FROM articles ar
LEFT JOIN author a ON ar.author_id = a.author_id
LEFT JOIN location l ON a.location_id = l.location_id
WHERE a.author_first_name = 'Sam' AND l.city <> 'Detroit'

SQL Joins with 4 tables

I have a problem here and I got a bit confused with outer/inner joins and multiple conditions
We have 4 tables with - columns:
table_cars - id | brand | type | license
table_equipments - id | name | description
table_distances - id_car | date | distance
table_cars_equipments - id_car | id_equipment
First query should show all cars that have equipment "fire extinguisher" and have been driving yesterday.
I have tried to write this query:
SELECT table_cars_equipments.id_car
FROM table_equipments
INNER JOIN table_cars_equipments
ON table_equipments.id = table_cars_equipments.id_equipment
AND table_equipments.name LIKE 'fire extinguisher';
Though I am still confused how to add the cars which had been driving yesterday, I don't know how to make the connection with the table table_distances.
Add another JOIN with the table table_cars and another one to the table table_distances. Then add a condition to the WHERE clause to get only those cars that have been driving yesterday. Something like this:
SELECT
c.id,
c.brand,
c.type,
c.license
ce.id_car,
...
from table_equipments AS e
INNER JOIN table_cars_equipments AS ce ON e.id = ec.id_equipment
INNER JOIN table_cars AS c ON c.id = ce.id_car
INNER JOIN table_distances AS d ON d.id_car = c.id
WHERE e.name LIKE 'fire extinguisher'
AND d.date = ?;
Note that: I used aliases for the table c, e etc, instead of the tables' full names.

How to write the right SQL query statement using join condition?

There are three fields in a table, all of them refer the same field in another table, like this:
table1
-------
! a_term_id* ! b_term_id* ! c_term_id* !
! ! ! !
table2
-------
! term_id ! term_title ! term_description !
! ------- ! ! !
columns a_term_id, b_term_id, c_term_id all refer to term_id
How should I write the SQL statement to retrieve the three fields' info?
I think you need to know how Sql_Join works. Here on W3Schools you can find useful examples.
A simple example:
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
INNER JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName
EDIT
You can try something like this:
SELECT * FROM tableA
inner join tableB on tableA.term_id = tableB.term_id
inner join tableC on tableA.term_id = tableC.term_id;
It an example you can modify as per your need.
Edit 2
SELECT * FROM tableB
JOIN tableA AS tableA1 ON tableB.term_id = tableA1.a_term_id
JOIN tableA AS tableA2 ON tableB.term_id = tableA2.b_term_id
JOIN tableA AS tableA3 ON tableB.term_id = tableA3.c_term_id
Here is an example. Suppose that we have two tables - Employees and Companies:
CREATE TABLE Employees(
Id int,
Name varchar(128),
CompanyId int);
CREATE TABLE Companies(
Id int,
Name varchar(128),
Address varchar(1024),
DateFounded datetime);
The following SQL query will join the tables:
SELECT * FROM Employees
INNER JOIN Companies
ON Employees.CompanyId = Companies.Id
Your question is a bit unclear, but I'll guess that you have a table A with three fields, each of which identifies a (possibly different) row from table B. You want to retrieve info from each of those rows of table B based on the field values of a single row of table A.
To do this, you will need to join table A to table B three times, once for each field of table A. Each join should be given an alias and then you can refer to the fields in the joined table by qualified field names.
SELECT b1.info, b2.info, b3.info
FROM A JOIN B AS b1 ON field1 = b1.field
JOIN B AS b2 ON field2 = b2.field
JOIN B AS b3 ON field3 = b2.field
WHERE ...
SELECT
t.a_term_id, a.term_title, a.term_description,
t.b_term_id, b.term_title, b.term_description,
t.c_term_id, c.term_title, c.term_description
FROM abc_terms t JOIN ( terms_info a, terms_info b, terms_info c )
ON ( t.a_term_id = a.term_id
AND t.b_term_id = b.term_id
AND t.c_term_id = c.term_id )