I am trying to join three tables, I joined two successfully.
This Worked (Two Tables)
SELECT ImportExportBadFile.ID, ImportExportSettingID,
ImportExportBadFile.UserID,
Coalesce(UserName,'') UserName
FROM
ImportExportBadFile
LEFT OUTER JOIN Users HGSQLUsers ON ImportExportBadFile.UserID = HGSQLUsers.ID
ORDER BY ImportExportBadFile.DateTimeStamp DESC
Now, when I tried to add another Table:
SELECT ImportExportBadFile.ID, ImportExportSettingID,
SETT.Name,
ImportExportBadFile.UserID,
Coalesce(UserName,'') HGSQLUsers.UserName
FROM
ImportExportBadFile
LEFT OUTER JOIN Users HGSQLUsers ON ImportExportBadFile.UserID = HGSQLUsers.ID
INNER JOIN ImportExportSettings SETT ON ImportExportBadFile.ImportExportSettingID = SETT.ID
ORDER BY ImportExportBadFile.DateTimeStamp DESC
It gives Error : "Incorrect syntax near '.'." at line : "Coalesce(UserName,'') HGSQLUsers.UserName"
If I write only UserName It gives
"Ambiguous column name 'UserName'."
Help.
Probably UserName column exists in both tables, you need to specify from which table do want to take UserName as below
SELECT ImportExportBadFile.ID, ImportExportSettingID,
SETT.Name,
ImportExportBadFile.UserID,
Coalesce( HGSQLUsers.UserName ,'') as "HGSQLUsers.UserName"
FROM
ImportExportBadFile
LEFT OUTER JOIN Users HGSQLUsers ON ImportExportBadFile.UserID = HGSQLUsers.ID
INNER JOIN ImportExportSettings SETT ON ImportExportBadFile.ImportExportSettingID = SETT.ID
ORDER BY ImportExportBadFile.DateTimeStamp DESC
Error "Incorrect syntax near '.'." at line : comes from alias HGSQLUsers.UserName you can use it but in quotas.
I also recommend you to use aliases for every table as below - is more readable for me
SELECT I.ID, I.ImportExportSettingID,
SETT.Name,
I.UserID,
Coalesce( HGSQLUsers .UserName ,'') as "HGSQLUsers.UserName"
FROM ImportExportBadFile I
LEFT OUTER JOIN Users HGSQLUsers ON I.UserID = HGSQLUsers.ID
INNER JOIN ImportExportSettings SETT ON I.ImportExportSettingID = SETT.ID
ORDER BY I.DateTimeStamp DESC
Actually this worked for me in trial and error:
SELECT ImportExportBadFile.ID, ImportExportSettingID,
SETT.Name,
ImportExportBadFile.UserID,
Coalesce(HGSQLUsers.UserName,'')
FROM
ImportExportBadFile
INNER JOIN ImportExportSettings SETT ON ImportExportBadFile.ImportExportSettingID = SETT.ID
LEFT OUTER JOIN Users HGSQLUsers ON ImportExportBadFile.UserID = HGSQLUsers.ID
ORDER BY ImportExportBadFile.DateTimeStamp DESC
but @Parado's Answer is more specific and explaining.
Thank you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With