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.
Related
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
...
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.
I have 3 tables (user, user_authority_rules_history & authority_rules_history) I want to select values from two of the tables if they match my criterias.
My query looks like this
SELECT
user_authority_rules_history.download_date,
authority_rules_history.*
FROM
user_authority_rules_history,
authority_rules_history
LEFT JOIN
user_authority_rules_history
ON
user_authority_rules_history.authority_rule_id = authority_rules_history.id
LEFT JOIN
user
ON
user_authority_rules_history.user_id = user.id
WHERE
user.id = 1
Im able make it work, if i leave out user_authority_rules_history.download_date from my query, but then im obviously missing that data. What am i doing wrong here ?
You have an extra table reference to user_authority_rules_history in the from clause. A simple rule: never use commas in the from clause.
I think this is what you intend:
SELECT user_authority_rules_history.download_date,
authority_rules_history.*
FROM authority_rules_history LEFT JOIN
user_authority_rules_history
ON user_authority_rules_history.authority_rule_id = authority_rules_history.id LEFT JOIN
user
ON user_authority_rules_history.user_id = user.id
WHERE user.id = 1
Note that you are filtering on user.id = 1. This is undoing your left join, so you might as well use inner join on the tables. I am not sure what you really intend, but this should fix your problem.
I have two tables for Groups and Category i wanted to return the groups and categories excluding those groups which doesn't have any category here is the SQL i wrote:
SELECT
cat_group.subject as group_name , spcat . *
FROM
special_event_groups AS cat_group
LEFT JOIN
special_event_categories AS spcat ON cat_group.id = spcat.group_id
AND cat_group.partner_id = spcat.partner_id;
Its returning me the records of group with NULL values which doesn't have any category. Do i need to use a subquery ?
What you need to do is to change LEFT JOIN to JOIN:
SELECT
cat_group.subject as group_name , spcat . *
FROM
special_event_groups AS cat_group
JOIN
special_event_categories AS spcat ON cat_group.id = spcat.group_id
AND cat_group.partner_id = spcat.partner_id;
If joining condition is not met, LEFT JOIN show row from special_event_groups and attach NULL values in selected columns from special_event_categories. JOIN, on the other hand, never returns row when JOIN condition is not met. You can read #MrMoose comment for more info.
You can use OUTER LEFT JOIN too, LEFT JOIN, JOIN, it depends on the version of your database.
Try this:
SELECT
cat_group.subject as group_name , spcat . *
FROM
special_event_groups AS cat_group
// LEFT JOIN, OUTER LEFT JOIN, JOIN
special_event_categories AS spcat ON cat_group.id = spcat.group_id
WHERE cat_group.partner_id IS NULL;
Let me know if it worked
I am running a query:
select course.course,iars.id,
students.rollno,
students.name as name,
teachers.name as tname,
students.studentid,
attndata.studentid ,sum(attndata.obt) as obt
sum(attndata.benefits) as ben , (sum(attndata.max)) as abc
from groups, students
left join iars
on iars.id
left join str
on str.studentid=students.studentid
left join course
on course.c_id=students.course
left join teachers
on teachers.id=iars.teacherid
join sgm
on sgm.studentid=students.studentid
left join attndata
on attndata.studentid=students.studentid and iars.id=attndata.iarsid
left join sps
on sps.studentid=students.studentid and iars.paperid=sps.paperid
left join semdef
on semdef.semesterid=str.semesterid
where students.course='1'
and students.status='regular'
and sps.paperid='5'
and iars.courseid=students.course
and iars.semester=str.semesterid
and semdef.month=9
and iars.paperid='5'
and str.semesterid='1'
and str.sessionid='12'
and groups.id=sgm.groupid
group by sps.studentid,
teachers.id,
semdef.month
order by
students.name
In this query whenever I am having left join on semdef.id=attndata.mon, I am getting zero result when the value of semdef.id=null but I want all the results, irrespective of semdef, but I want to use it. As in it should fetch result, if the values are null. Can you please help it out.
It's probably because your where clause is saying
and semdef.month=9
and you probably want
and (semdef.month=9 OR semdef.id IS NULL)
or something similar.
It's because your where clause has statements relating to the semdef table. Add these to the join clause as putting these in the where is implying an inner join.
Eg:
Left join semdef on xxx and semdef.id = attndata.min