Error code 1066 sqlstate 42000 not unique table alias - mysql

I am getting this error call not unique table alias, Im not able to figure out the issue. I have a common date table connecting all. Also teacher table connecting two tables - Leave and Attendance. Please help
SELECT
trns_teacherattendance.Attendance_Status,
trns_teacherattendance.Attendance_Month,
trns_teacherattendance.AcademicYear_Id,
trns_teacherattendance.School_Id,
trns_teacherattendance.Bio_Code,
trns_teacherattendance.IsActive,
mst_holiday_teacher.Holiday_Name,
dates.dates,
leave_new_view_teacher.Leave_Status,
mst_teacher.Teacher_Name,
leave_new_view_teacher.LeaveDate
FROM
trns_teacherattendancemapping
LEFT OUTER JOIN
trns_teacherattendance
ON
(
trns_teacherattendancemapping.Bio_Code =
trns_teacherattendance.Bio_Code)
RIGHT OUTER JOIN
dates
ON
(
trns_teacherattendance.Attendance_Date = dates.dates)
LEFT OUTER JOIN
mst_teacher
ON
(
trns_teacherattendancemapping.Teacher_Id =
mst_teacher.Teacher_Id)
RIGHT OUTER JOIN
leave_new_view_teacher
ON
(
mst_teacher.Teacher_Id = leave_new_view_teacher.TID)
LEFT OUTER JOIN
leave_new_view_teacher
ON
(
dates.dates = leave_new_view_teacher.LeaveDate)
LEFT OUTER JOIN
mst_holiday_teacher
ON
(
dates.dates = mst_holiday_teacher.Holiday_Date) ;

You are joining twice to a table leave_new_view_teacher but you aren't giving it different aliases. Database can't know from which query/table you would like to reference your columns. Each join could pull different data/rows.
Assign aliases to these tables as a minimum requirement in this part:
RIGHT OUTER JOIN
leave_new_view_teacher AS lnvt1 -- here
ON
mst_teacher.Teacher_Id = leave_new_view_teacher.TID
LEFT OUTER JOIN
leave_new_view_teacher AS lnvt2 -- and here
ON
dates.dates = leave_new_view_teacher.LeaveDate
You will also need to properly classify columns in SELECT part:
SELECT
...
lnvt1.Leave_Status, -- you're probably getting this from first join
mst_teacher.Teacher_Name,
lnvt2.LeaveDate -- this probably comes from second join
You also don't need the parentheses for each ON clause, so I've removed them.

Related

Error # 1066 - not a unique table / alias in MySQL

Good afternoon, I’m trying to fulfill the request while writing an error. Error # 1066 does not quite understand how it can be fixed in my particular case. Perhaps the problem is that I connect to the table several times and need an alias.
SELECT `employees`.`name`, `employees`.`surname`, `employees`.`patronymic`,
`doc`.`name`, `doc`.`agreement`, `tank`.`name`,
`liquid`.`name`, `WorkPlan`.`description`
FROM `WorkPlan` , `employees` , `doc` , `tank` , `liquid`
LEFT JOIN `WorkPlan` ON `tank`.`id` = `WorkPlan`.`id_tank`
LEFT JOIN `WorkPlan` ON `liquid`.`id` = `WorkPlan`.`id_liquid`
LEFT JOIN `WorkPlan` ON `doc`.`id` = `WorkPlan`.`id_doc`
AND `WorkPlan`.`id_tank` = `tank`.`id`
AND `WorkPlan`.`id_liquid` = `liquid`.`id`
AND `WorkPlan`.`id_doc` = `doc`.`id`
I suspect (by your SELECT list of columns) that you want to join employees to the other tables.
For every join you must specify in the ON clause the columns that relate the 2 tables and it is a good practice to use aliases for the tables which shorten the code and make it more readable:
SELECT e.name, e.surname, e.patronymic,
d.name, d.agreement,
t.name,
l.name,
w.description
FROM employees e
LEFT JOIN WorkPlan w ON e.? = w.?
LEFT JOIN tank t ON t.id = w.id_tank
LEFT JOIN liquid l ON l.id = w.id_liquid
LEFT JOIN doc d ON d.id = w.id_doc
Replace the ? with the names of the columns that relate employees with WorkPlan.

How can I populate a table when inner join values might be null?

I'm populating a table which is fetching the ids from 2 other tables to display their information, for example, delivery has a Hamburguer and the box, but the user might register the delivery with out the box, only with the hamburguer.
When I make a INNER JOIN SELECT to get the data from the DB it will return 0 results since there is no box and I'm trying to compare the ids that don't exist. It doesn't populate the table then.
SELECT
entrega_telemovel.*,
telemovel.id_telemovel,
telemovel.nroserie,
nro_telemovel.numero_telemovel,
nro_telemovel.id_nrotelemovel,
funcionarios.id_funcionario,
funcionarios.nome
FROM entrega_telemovel
INNER JOIN telemovel
ON entrega_telemovel.telemovel = telemovel.id_telemovel
INNER JOIN nro_telemovel
ON nro_telemovel.id_nrotelemovel = entrega_telemovel.numero_telemovel
INNER JOIN funcionarios
ON funcionarios.id_funcionario = entrega_telemovel.funcionario_entrega
ORDER BY funcionarios.nome;
In this query above entrega_telemovel.telemovel=telemovel.id_telemovel the value in entrega_telemovel.telemovel is null like the example I gave above. So 0 results are returned from the query.
How can I solve this ?
You are looking for a LEFT JOIN.
INNER JOIN only combines rows, that exist in both tables. A LEFT JOIN on the other hand always produces at least one row. If on table does not have a match for it, all columns are set to NULL.
SELECT
entrega_telemovel.*,
telemovel.id_telemovel,
telemovel.nroserie,
nro_telemovel.numero_telemovel,
nro_telemovel.id_nrotelemovel,
funcionarios.id_funcionario,
funcionarios.nome
FROM entrega_telemovel
LEFT JOIN telemovel
ON entrega_telemovel.telemovel = telemovel.id_telemovel
LEFT JOIN nro_telemovel
ON nro_telemovel.id_nrotelemovel = entrega_telemovel.numero_telemovel
LEFT JOIN funcionarios
ON funcionarios.id_funcionario = entrega_telemovel.funcionario_entrega
ORDER BY funcionarios.nome;
You want to show all entrega_telemovel entries, no matter whether they have a match in entrega_telemovel or not. This is what an outer join does.
SELECT ...
FROM entrega_telemovel et
LEFT OUTER JOIN telemovel t ON et.telemovel = t.id_telemovel
...

how to use "USING" keyword to join three table and display it using mysql syntax

this is how i manage to join the table using inner join
SELECT lab5enrollment.matricno, lab5student.stuname,
lab5enrollment.courseid,
lab5course.cname
FROM ((lab5enrollment
INNER JOIN lab5student ON lab5enrollment.matricno = lab5student.matricno)
INNER JOIN lab5course ON lab5enrollment.courseid = lab5course.courseid)
WHERE lab5enrollment.courseid = 'CSF3402';
this is how i used the using keyword to join the table but i dont know how to join the three table...
SELECT matricno, stuname, courseid, cname
FROM lab5enrollment
JOIN lab5student
USING (matricno)
WHERE courseid = 'CSF3402';
i want to observe the differrence between using the inner join and using...
You should probably lean towards using joins with explicit ON clauses for a number of reasons. If you wanted to use USING here, then the following should work:
SELECT
t1.matricno,
t2.stuname,
t1.courseid,
t3.cname
FROM lab5enrollment t1
INNER JOIN lab5student t2
USING (matricno)
INNER JOIN lab5course t3
USING (courseid)
WHERE
t1.courseid = 'CSF3402';
This assumes that lab5enrollment and lab5student both have a column with the same name matricno, and that lab5student and lab5course both have a column called courseid.

Not unique table/alias

So I'm trying to run this query with multiple joins to get exact row as I want but I keep getting this error
Not unique table/alias: 'ss_prices'
The query I'm running:
select `ss_accounts`.`id`,
`ss_accounts`.`bot_acc_id`,
`ss_accounts_inventories`.*,
`ss_prices`.*
from `ss_accounts_inventories`
left join `ss_prices`
on `ss_accounts_inventories`.`item_name` = `ss_prices`.`item_name`
left join `ss_prices`
on `ss_accounts_inventories`.`phase` = `ss_prices`.`item_phase`
inner join `ss_accounts`
on `ss_accounts_inventories`.`bot_id` = `ss_accounts`.`id`
order by `ss_prices`.`item_price` DESC
You're joining the same table twice:
...
left join `ss_prices` on ...
left join `ss_prices` on ...
...
So anywhere else in the query when you reference that table, the query engine has no way to know which one you mean. For example, in your select clause:
`ss_prices`.*
Give each joined table an alias so you can distinguish them:
...
left join `ss_prices` as item_name_prices on ...
left join `ss_prices` as phase_prices on ...
...
Then reference them by their aliases in the rest of the query:
select
`ss_accounts`.`id`,
`ss_accounts`.`bot_acc_id`,
`ss_accounts_inventories`.*,
`item_name_prices`.*, -- here
`phase_prices`.* -- and here, etc.

mysql right join not working as expected

When I use right join I get the same results as using left join or just join. Can anyone show me where I have gone wrong?
I have 3 tables as follows:
langugages
id
code eg "hu","en"
language_default
id
text
language_translations
id
lang_id (FK the id of the language in the languages table)
default_lang_id (FK the id of the text in the languages_default table)
text (the translation)
When I execute the following query, I expect to get all of the hungarian translations from the language_translations table and all of the text fields from the language_default table with a null value where there is no hungarian translation.
SELECT `language_translations`.`text`
, `language_default`.`text`
FROM `languages`
, `language_translations`
RIGHT JOIN `language_default` ON `language_default`.`id` = `language_translations`.`default_lang_id`
WHERE `languages`.`code` = 'hu'
AND `languages`.`id` = `language_translations`.`lang_id`
Instead I only get text from the language_default table where there are translations for that text in the tranlsation table. I would expect that behaviour from a left join or normal join but not a right join. Any ideas why I am not getting all of the entries from the langugage_defailt table?
First of all you are using combination of normal join and rights join in wrong way. You can use as per below query.
2nd thing right join means you will get all record from right side and corresponding records from left side and if left side does not have corresponding record then it will show NULL.
Left join is its reverse.
Normal join or comma join will provide only common rows.
So if your tables only have common rows then all joins will provide same results.
SELECT
`language_translations`.`text` , `language_default`.`text`
FROM `languages` AS l
JOIN `language_translations` AS t
ON l.`id` = t.`lang_id` AND l.`code` = 'hu'
RIGHT JOIN `language_default` AS d
ON d.`id` = t.`default_lang_id`;
Don't mix implicit joins and explicit joins. A simple rule is: don't use , in the from clause. The following restructures your query to use left outer join:
SELECT `language_translations`.`text`, ld.`text`
FROM language_default ld left outer join
language_translations lt
on ld.`id` = lt.`default_lang_id` left outer join
`languages` l
on l.`id` = lt.`lang_id` and l.`code` = 'hu' ;
When using outer joins, you need to be very careful about where additional conditions go. Conditions on the driving table (the first a left outer join, the last for a right outer join) can go in the where clause. For other tables, the conditions should go in the on clause. Otherwise, they turn the outer join into an inner for the simple reason that (almost) any comparison to NULL is equivalent to false.