I have a three tables.
Alan1, Alan2, Alan3 are the same in all tables.I want to merge tables. The difference of the tables is the rightmost column. How should I write a SQL Query?
You can join. As commented by Barmar, the idea is to use the first 3 columns as join keys;
select a.*, b.alan4 as alan4b, c.alan4 as alan4c
from a
inner join b
on b.alan1 = a.alan1
and b.alan2 = a.alan2
and b.alan3 = a.alan3
inner join c
on c.alan1 = a.alan1
and c.alan2 = a.alan2
and c.alan3 = a.alan3
This gives you rows that are available in all 3 tables. Say you want to allow "missing" rows in b and/or c, then you need to change the two inner joins to left joins.
Related
I am facing some difficulty when using 'OR' queries with multi-table joins with MySql 5.7.
For example, let's say I want a list of Animals that are either currently at a location or are being shipped there.
Select * from animals
left outer join animal_locations on animals.id = animal_locations.animal_id
left outer join animal_shipments on animals.id = animal_shipments.animal_id
where animal_locations.location_id = 5 OR animal_shipments.location_id = 5
The queries are very fast when using "AND" as well as using individual columns without any operator. But when "OR" queries are used, there is a big performance hit, the right indexes are not being hit.
Any way I can achieve this without splitting it up into multiple queries?
SELECT *
FROM animals
LEFT OUTER JOIN animal_locations ON animals.id = animal_locations.animal_id
AND animal_locations.location_id = 5
LEFT OUTER JOIN animal_shipments ON animals.id = animal_shipments.animal_id
AND animal_shipments.location_id = 5
WHERE COALESCE(animal_locations.animal_id, animal_shipments.animal_id) IS NOT NULL
The logic - previously the rows in slave tables with location_id = 5 are filtered, and then filtered rows are joined to main table.
I am using a left join to get data into a single table with one query, I have done this for two different table and am now writing a single query to fetch data into a single one. My query is:
select ec.CUSTOMERDESCRIPTOR as OUTLET,
ollastsyncdatetime.TIMESTMP as LASTSYNCEDDATETIME,
ollastindentdatetime.TIMESTMP as LASTINDENTDATETIME
from (ollastsyncdatetime
left join ollastindentdatetime on ((ollastsyncdatetime.OUTLET = ollastindentdatetime.OUTLET)))
Inner Join ecustomer ec on ollastsyncdatetime.outlet = ec.CUSTOMERIDENTIFIER
group by ollastsyncdatetime.OUTLET
This query is giving me a result like this:
I am able to join two tables but facing issues in joining more than two.
Now I have 2 more tables in my Database from which I have to fetch data and make new columns
the two new tables are
ollastdumpdamagedatetime
ollastindentdatetime
I want a result like this:
If there is no data in table it should show blank because I am populating columns data on the comparison with outlets. Please ignore the actual data in table; they are same because I am using this as an example only.
Use left join for both table
select ec.CUSTOMERDESCRIPTOR as OUTLET,ollastsyncdatetime.TIMESTMP as LASTSYNCEDDATETIME,ollastindentdatetime.TIMESTMP as LASTINDENTDATETIME
from ollastsyncdatetime left join ollastindentdatetime
on ollastsyncdatetime.OUTLET = ollastindentdatetime.OUTLET
left Join ecustomer ec
on ollastsyncdatetime.outlet = ec.CUSTOMERIDENTIFIER
I am pretty new in SQL and I have the following problem with a simple query.
I have to perform a query that JOIN 2 tables. The join operation is not on a single field but have to be performed on 2 differents field having the same name on these tables.
So I have done something like this:
SELECT count(*)
FROM TID003_ANAGEDIFICIO anagraficaEdificio
INNER JOIN TID002_CANDIDATURA candidatura ON(candidatura.PRG_PAR = anagraficaEdificio.PRG_PAR, candidatura.PRG_CAN = anagraficaEdificio.PRG_CAN)
WHERE anagraficaEdificio.FLG_GRA=1;
But, performing this query, I obaint the following error message:
Operand should contain 1 column(s)
As you can see I am trying to join these 2 tables and the join is based on 2 fields, by:
ON(candidatura.PRG_PAR = anagraficaEdificio.PRG_PAR, candidatura.PRG_CAN = anagraficaEdificio.PRG_CAN)
What is wrong? What am I missing? How can I fix this issue?
Use AND to specify the additional join conditions if you want to use more than one condition together:
SELECT count(*)
FROM TID003_ANAGEDIFICIO anagraficaEdificio
JOIN TID002_CANDIDATURA candidatura
ON candidatura.PRG_PAR = anagraficaEdificio.PRG_PAR
AND candidatura.PRG_CAN = anagraficaEdificio.PRG_CAN
WHERE anagraficaEdificio.FLG_GRA = 1;
If the column names of the joined columns are the same in both tables you could use a join with the USING predicate instead:
JOIN TID002_CANDIDATURA candidatura USING (PRG_PAR, PRG_CAN)
This is not supported in all databases, but it is in MySQL (see the documentation for more information).
I need to join four tables into one on some conditions, but I only manage to join three of them, even if I do everything exactly the same on the fourth one. Can somebody, please, help me with this issue? It works if I delete the last paragraph, but if I leave it there it says "syntax error in JOIN operation".
SELECT Leidinys, ISSN, Pobudis
FROM ((Leidinio_ID_Leidinys
LEFT JOIN (Leidinio_ID_ISSN_ID
LEFT JOIN ISSN_ID_ISSN
ON Leidinio_ID_ISSN_ID.ISSN_ID = ISSN_ID_ISSN.ISSN_ID)
ON (Leidinio_ID_Leidinys.Leidinio_ID = Leidinio_ID_ISSN_ID.Leidinio_ID))
LEFT JOIN ((Leidinio_ID_Pobudzio_ID
LEFT JOIN Pobudzio_ID_Pobudis
ON Leidinio_ID_Pobudzio_ID.Pobudzio_ID = Pobudzio_ID_Pobudis.Pobudzio_ID))
ON (Leidinio_ID_Leidinys.Leidinio_ID = Leidinio_ID_Pobudzio_ID.Leidinio_ID))
LEFT JOIN ((Leidinio_ID_Metai_ID
LEFT JOIN Metai_ID_Prieigos_Metai
ON Leidinio_ID_Metai_ID.Metai_ID = Metai_ID_Prieigos_Metai.Metai_ID)
ON (Leidinio_ID_Leidinys.Leidinio_ID = Leidinio_ID_Metai_ID.Leidinio_ID))
your parens are out of sync i bet
you need the first parens after first FROM to enclose all 3 "left joins" clauses, so you need to
1) copy to clipboard then remove the last "left join clause"
2) insert the copied code in front of the very last parens
this is my best guess based on the info provided, good luck
Not a complete answer but a pointer the table names are so similar to each other and also they r not in english so struggling a bit , but here is something you need to do .
JOIN between two table at one time and then mention their joining condition in ON clause
SELECT Leidinys, ISSN, Pobudis
FROM Leidinio_ID_Leidinys LEFT JOIN Leidinio_ID_ISSN_ID --<-- Two Table in JOIN
ON Leidinio_ID_Leidinys.Leidinio_ID = Leidinio_ID_ISSN_ID.Leidinio_ID --<-- Condition on you want to join these table
LEFT JOIN ISSN_ID_ISSN --<-- result set on 1st join, joins with this table
ON Leidinio_ID_ISSN_ID.ISSN_ID = ISSN_ID_ISSN.ISSN_ID --<-- condition on which you want to join the result set and this table ... and so on ....
LEFT JOIN Leidinio_ID_Pobudzio_ID
ON Leidinio_ID_Leidinys.Leidinio_ID = Leidinio_ID_Pobudzio_ID.Leidinio_ID
LEFT JOIN Pobudzio_ID_Pobudis
ON Leidinio_ID_Pobudzio_ID.Pobudzio_ID = Pobudzio_ID_Pobudis.Pobudzio_ID
.
...... Join with other tables along with their joining conditions and so on.....
Then also in your Select statement you need to mention the TableName.Column name because the column names you have mentioned in your Select statement exists in more then One table you need to tell sql server from which table you need that particular column.
SELECT
description
FROM
diagnosis_mapping
LEFT JOIN
diagnosis_codes
ON
diagnosis_codes.codeid = diagnosis_mapping.codeid
SELECT
description
FROM
diagnosis_mapping
LEFT JOIN
diagnosis_codes
ON
diagnosis_codes.codeid = diagnosis_mapping.secondarycodeid
How to merge these 2 queries and get info in a single resultset?
In first i need to match with codeid and in second i need to match with secondarycodeid to the same mastertable to fetch the description of both.
You can do two joins in one query, just give an alias to the table names so MySQL knows what you want to get:
SELECT
a.description desc_a,
b.description desc_b
FROM
diagnosis_mapping
LEFT JOIN
diagnosis_codes a
ON
a.codeid = diagnosis_mapping.codeid
LEFT JOIN
diagnosis_codes b
ON
b.codeid = diagnosis_mapping.secondarycodeid
In this example, a is an alias for the first diagnosis_codes table and b to the other. When you give alias names to the tables, MySQL (and any other SQL aware database) treats them basically as two separate tables and fetches data from them independently.
SELECT description FROM (diagnosis_mapping LEFT JOIN diagnosis_codes ON (diagnosis_codes.codeid = diagnosis_mapping.codeid)) LEFT JOIN diagnosis_mapping ON (diagnosis_codes.codeid = diagnosis_mapping.secondarycodeid)
Not sure if thats what you need, but try ;] (might need some spelling checking and adding some "AS" if there are any sql errors)