mysql right join not working as expected - mysql

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.

Related

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
...

Error code 1066 sqlstate 42000 not unique table alias

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.

joining tables in mysql while one of them is not reachable

I wrote a query that join 3 tables :
SELECT `notification`.`nid`, `notification`.`type`, `notification`.`read`, `notification`.`time`, `user`.`pro_img`, `user`.`fname` ,`user`.`lname`, `user`.`username`, `accommodation`.`aid`, `accommodation`.`title`, `accommodation`.`home_img`
FROM `notification` RIGHT OUTER JOIN `user` ON `notification`.`rid`=`user`.`id` RIGHT OUTER JOIN `accommodation` ON `notification`.`acco_id`=`accommodation`.`aid`
WHERE `notification`.`uid`=$uid ORDER BY `notification`.`time` DESC;
i want to have result while notification.acco_id= accommodation.aid is not found so empty columns is ok but i want rows
Your query is combining a few things that don't go along. The right joins you are using will preserve anything that's in the right side tables, in your case user and accomodation, but using two of them with the same left side will make this useless. You also have a condition in your where involving the left side of the joins, and this also will make the right joins pointless.
Assuming you want all the notifications, regardless of them being matched with users and accomodations or not, you only need to change from right to left joins.
SELECT notification.nid,
notification.type,
notification.read,
notification.time,
user.pro_img,
user.fname,
user.lname,
user.username,
accommodation.aid,
accommodation.title,
accommodation.home_img
FROM notification
LEFT OUTER JOIN
user
ON notification.rid = user.id
LEFT OUTER JOIN
accommodation
ON notification.acco_id = accommodation.aid
WHERE notification.uid = $uid
ORDER BY notification.time DESC
This will preserve all the rows in notification, and will match them with null values when there are no matching rows on the other table(s).

inner and outer join together in query, possible

i am trying to implement inner and outer join in single query, i am not sure if i am doing the right way or wrong way, as i am not very good with queries.
So here it goes.
I have these following tables.
hrs_residentials
hrs_residential_utilities
hrs_utilities
hrs_utility_type
hrs_residentials:
ResID, ResType, ResNo - - -
1 2 001 - - -
hrs_residential_utilities:
RUID, UtilityID, ResID, - - - -
NULL NULL NULL
hrs_utilities:
UtilityID, UtilityTypeID, Number, ConsumerNumber, -, -, -
NULL NULL NULL NULL
hrs_utility_type:
UTID, UName, UDescription
1 PESCO PESCO Electric Meter
2 SNGPL Sui Northen Gas Pipe Lines
So i want to show in datatables the data, but what i want that data should show in table for hrs_residentials table, dosent matter if hrs_residential_utilities have data or not.
So i went for Left outer join and i got the result i wanted.
But after that when i tried to do inner for hrs_residential_utilities with hrs_utilities, i stopped getting results for hrs_residentials as well. As if we see hrs_residential do have the data inside table. I dont want inner join with hrs_residentials, i want to have inner join between hrs_residential_utilities and hrs_utilities.
Is it possible, or i am following the wrong approach here? Sorry i am not good. What will the Proper Query if anyone can help me with it.
This is the Query i have tried so far.
SELECT R.`ResID`,R.`ResNo`
FROM `hrs_residentials` R
LEFT OUTER JOIN `hrs_residential_utilities` RU
ON R.`ResID` = RU.ResID
INNER JOIN `hrs_utilities` U
ON RU.`UtilityID` = U.`UtilityID`
WHERE 1=1;
I stopped Getting Results from the hrs_residentials table After the Inner Join, but i am making Inner join between other two tables.
Try a subquery like this:
SELECT *
FROM `hrs_residentials` R
LEFT OUTER JOIN
(
SELECT * FROM
`hrs_residential_utilities` RU
INNER JOIN `hrs_utilities` U
ON RU.`UtilityID` = U.`UtilityID`
) AS subqyr
ON R.`ResID` = subqyr.`ResID`
run this query and view the results
SELECT R.ResID,R.ResNo
FROM hrs_residentials R
LEFT OUTER JOIN hrs_residential_utilities RU
ON R.ResID = RU.ResID
then run this query and view the results:
SELECT * FROM hrs_utilities
I suspect what you will find is that the RU.utilities ID does not match anything in the hrs_utilities.
Your query itself should return everything in hrs_residentials and join on any matching data from hrs_residential_utilities this may or may not return RU.ResID as null depending on whether it can match.
It then filters on whether RU.UtilityId matches with anything in the hrs_utilities table, this won't match on null items.
Thanks.
oliver

How to join three tables in Oracle where one of the table has null values in the foreign key column?

I have to execute the following:
select items.segment1
from items
,po_lines
,po_shipments
where po_lines.item_id = items.item_id(+) --po_lines has null in some item_id
po_shipments.ship_to = items.org_id;
But the outer join seem to be not working. The query should return 100 values while it is returning only 85. It is neglecting the 15 for which po_lines.item_id is null.
Any help would be appreciated.
The (+) operator should be applied to the table that's allowed to have "missing" rows - you have it on the wrong side of the join.
select items.segment1
from items
,po_lines
,po_shipments
where po_lines.item_id (+) = items.item_id --po_lines has null in some item_id
po_shipments.ship_to = items.org_id;
Quoting Wikipedia JOIN (SQL):
ANSI-standard SQL specifies five types of JOIN: INNER, LEFT OUTER,
RIGHT OUTER, FULL OUTER and CROSS
You can use left join in this way:
select items.segment1
from
items
left outer join
po_lines on po_lines.item_id = items.item_i
inner join
po_shipments on po_shipments.ship_to = items.org_id;
Your oracle (*) syntax is deprecated:
Starting with Oracle9i, the confusing outer join syntax using the
‘(+)’ notation has been superseded by ISO 99 outer join syntax.