I can't join multiple tables on SQL - mysql

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.

Related

MySQL - Why does WHERE ignore RIGHT JOIN?

I have the following MySQL query:
SELECT inv.inventory_id, inv.item_id, item.description, inv.quantity, item.class_id, class.description AS class,
class.is_spool, inv.location_id, location.description AS location, location.division_id, division.name AS division,
inv.service_date, inv.reel_number, inv.original_length, inv.current_length, inv.outside_sequential,
inv.inside_sequential, inv.next_sequential, inv.notes, inv.last_modified, inv.modified_by
FROM reel_inventory AS inv
INNER JOIN reel_items AS item ON inv.item_id = item.item_id
INNER JOIN reel_locations AS location ON inv.location_id = location.location_id
INNER JOIN locations AS division ON location.division_id = division.location_id
RIGHT JOIN reel_classes AS class on item.class_id = class.class_id;
The query works exactly as expected as is. What I was trying to do was add a WHERE clause to this query with one qualifier. For example:
RIGHT JOIN reel_classes AS class ON item.class_id = class.class_id
WHERE inv.current_length > 0;
When I do this, all of the results from the RIGHT JOIN are not included in the result. I've not had a ton of experience with advanced queries, but could someone explain why the RIGHT JOIN is excluded from the result set when a WHERE is used, and how to property write the query to include the RIGHT JOIN information?
Thanks in advance.
What you want is:
RIGHT JOIN reel_classes AS class
ON item.class_id = class.class_id AND
inv.current_length > 0;
Your question is why the RIGHT JOIN turns into an INNER JOIN with the WHERE clause.
The reason is simple. For the non-matching rows, inv.current_length is NULL and this fails the comparison.
I would also suggest that you use LEFT JOIN, starting with the table where you want to keep all the rows. Most people find it much easier to understand logic that is "keep all rows in the first table" rather than "keep all rows in some table whose name will come up".

Retrieving uniq records with union in mysql

I have two tables usersin and usersout(I can not change schema, a lot of system changes must be done in php otherwise). I should get all user records in a query but I should mark them if they are in or out also a user may have an in record and out record I shouldn't show in record if has an out record.
I have created tables with sample data in SQL Fiddle: http://www.sqlfiddle.com/#!9/ac99a/1/0
Can u help me how can I remove duplicates of user records in this union query?
If you want to have all entries with an entry in either the in or out table, but not in both of them, then a full outer join would be your friend.
Since MySQL does not know that kind of join, you can emulate it with a left outer join and a right outer join combined like so:
SELECT
ui.id, ui.user, 'i'
FROM
usersIN ui
LEFT OUTER JOIN
usersOUT uo ON ui.user = uo.user
WHERE uo.id IS NULL
UNION
SELECT
uo.id, uo.user, 'o'
FROM
usersIN ui
RIGHT OUTER JOIN
usersOUT uo ON ui.user = uo.user
WHERE ui.id IS NULL;
This should give you the right output.
A good visual explanation of joins can be found here

LEFT JOIN two columns from one table on two separate tables

I'm not sure if this is possible exactly as stated in the title, but what I'm trying to accomplish, however possible, is what would amount to left joining two columns from one table, each on a separate table.
Here's the statement I'm working with. Unfortunately, MySQL doesn't allow me to actually do this and gives me an error message. Is there another way to accomplish what I want? Is my syntax perhaps just off?
select h.HITTER_ID, u.UMPIRE_ID, u.HAND_B
from
hitters h,
schedules sched
LEFT JOIN
umpires u
ON sched.home_base_umpire_id = u.UMPIRE_ID and h.HAND_B = u.HAND_B
In the umpires table, each UMPIRE_ID appears twice, once with HAND_B = "R" and once with HAND_B = "L"
Essentially, I want to:
1) Pull the UMPIRE_ID from umpires when that UMPIRE_ID appears in schedules
2) Of the two UMPIRE_ID records, select the one with the HAND_B field that corresponds to the HAND_B field in hitters
I could put the "h.HAND_B = u.HAND_B" in the where clause, but that would require that the UMPIRE_ID not be NULL, and I need to leave open the possibility that it is NULL.
How can I accomplish this?
You need to modify your query to be like below
select h.HITTER_ID,
u.UMPIRE_ID,
u.HAND_B
from hitters h
left join umpires u on h.HAND_B = u.HAND_B
left join schedules sched ON sched.home_base_umpire_id = u.UMPIRE_ID;

How do I decide when to use right joins/left joins or inner joins Or how to determine which table is on which side?

I know the usage of joins, but sometimes I come across such a situation when I am not able to decide which join will be suitable, a left or right.
Here is the query where I am stuck.
SELECT count(ImageId) as [IndividualRemaining],
userMaster.empName AS ID#,
CONVERT(DATETIME, folderDetails.folderName, 101) AS FolderDate,
batchDetails.batchName AS Batch#,
Client=#ClientName,
TotalInloaded = IsNull(#TotalInloaded,0),
PendingUnassigned = #PendingUnassigned,
InloadedAssigned = IsNull(#TotalAssigned,0),
TotalProcessed = #TotalProcessed,
Remaining = #Remaining
FROM
batchDetails
Left JOIN folderDetails ON batchDetails.folderId = folderDetails.folderId
Left JOIN imageDetails ON batchDetails.batchId = imageDetails.batchId
Left JOIN userMaster ON imageDetails.assignedToUser = userMaster.userId
WHERE folderDetails.ClientId =#ClientID and verifyflag='n'
and folderDetails.FolderName IN (SELECT convert(VARCHAR,Value) FROM dbo.Split(#Output,','))
and userMaster.empName <> 'unused'
GROUP BY userMaster.empName, folderDetails.folderName, batchDetails.batchName
Order BY folderDetails.Foldername asc
Yes, it depends on the situation you are in.
Why use SQL JOIN?
Answer: Use the SQL JOIN whenever multiple tables must be accessed through an SQL SELECT statement and no results should be returned if there is not a match between the JOINed tables.
Reading this original article on The Code Project will help you a lot: Visual Representation of SQL Joins.
Also check this post: SQL SERVER – Better Performance – LEFT JOIN or NOT IN?.
Find original one at: Difference between JOIN and OUTER JOIN in MySQL.
In two sets:
Use a full outer join when you want all the results from both sets.
Use an inner join when you want only the results that appear in both
sets.
Use a left outer join when you want all the results from set a, but
if set b has data relevant to some of set a's records, then you also
want to use that data in the same query too.
Please refer to the following image:
I think what you're looking for is to do a LEFT JOIN starting from the main-table to return all records from the main table regardless if they have valid data in the joined ones (as indicated by the top left 2 circles in the graphic)
JOIN's happen in succession, so if you have 4 tables to join, and you always want all the records from your main table, you need to continue LEFT JOIN throughout, for example:
SELECT * FROM main_table
LEFT JOIN sub_table ON main_table.ID = sub_table.main_table_ID
LEFT JOIN sub_sub_table on main_table.ID = sub_sub_table.main_table_ID
If you INNER JOIN the sub_sub_table, it will immediately shrink your result set down even if you did a LEFT JOIN on the sub_table.
Remember, when doing LEFT JOIN, you need to account for NULL values being returned. Because if no record can be joined with the main_table, a LEFT JOIN forces that field to appear regardless and will contain a NULL. INNER JOIN will obviously just "throw away" the row instead because there's no valid link between the two (no corresponding record based on the ID's you've joined)
However, you mention you have a where statement that filters out the rows you're looking for, so your question on the JOIN's are null & void because that is not your real problem. (This is if I understand your comments correctly)

Mysql join query

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)