I was sure to implement yesterday and Have used LEFT JOIN if the other table has no value.
My Problem now is What if you are unsure which table has value and which table has value.
I have tried this query.
$query ="SELECT record.student_name,record.student_id,record.student_finger,record.student_section,record.activity_type,record.activity_title,record.score,record.teacher_id,record.activity_id,record.subject_id,attendance.status,attendance.date
FROM tbl_record record
LEFT JOIN tbl_attendance attendance on record.student_id=attendance.student_number
WHERE record.subject_id='$subject_id' and record.teacher_id='$teacher_id' and record.student_section='$section';";
But Unfortunately, If my record table is empty, It will not show anything.
What Im tryng to achieve is,
If table_record is empty and tbl_attendance is not, it will show records in tbl_attendance,
and if table_attendance is empty and table_record is not, it will show records in table_record.
Change LEFT JOIN to FULL OUTER JOIN, adapt the WHERE clause and let us know how it goes.
SELECT
record.student_name,
coalesce(record.student_id, attendance.student_number),
record.student_finger,
record.student_section,
record.activity_type,
record.activity_title,
record.score,
record.teacher_id,
record.activity_id,
record.subject_id,
attendance.status,
attendance.date
FROM tbl_record record
FULL OUTER JOIN tbl_attendance attendance on record.student_id=attendance.student_number
WHERE (record.subject_id='$subject_id' and record.teacher_id='$teacher_id'
and record.student_section='$section')
or record.subject_id is null;
This is a draft MySql version, where FULL OUTER JOIN is not supported. You can replace all record.* fields with NULLs in the RIGHT JOIN part:
SELECT
record.student_name,
record.student_id,
record.student_finger,
record.student_section,
record.activity_type,
record.activity_title,
record.score,
record.teacher_id,
record.activity_id,
record.subject_id,
attendance.status,
attendance.date
FROM tbl_record record
LEFT JOIN tbl_attendance as attendance on
record.student_id = attendance.student_number
WHERE (record.subject_id='$subject_id' and record.teacher_id='$teacher_id'
and record.student_section='$section')
UNION
SELECT
record.student_name,
attendance.student_number,
record.student_finger,
record.student_section,
record.activity_type,
record.activity_title,
record.score,
record.teacher_id,
record.activity_id,
record.subject_id,
attendance.status,
attendance.date
FROM tbl_record as record
RIGHT JOIN tbl_attendance as attendance on
record.student_id = attendance.student_number
WHERE record.subject_id is null
;
Just Change "Left JOIN" to "FULL OUTER JOIN"
SELECT *
FROM tbl_Students S
FULL OUTER JOIN tbl_Attendance A
ON S.StudentID = A.AttendanceStudentID
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 have 6 queries like the following query listed below..
each are taking 6 seconds to run
for a total of 36 seconds for page to load
Is there a way to optimize these kinds of queries?
SELECT
tickets.ticketID,
tickets.ticket,
tickets.name1,
tickets.address1,
tickets.city,
tickets.cstate,
tickets.zip,
tickets.caller_type,
tickets.phone,
tickets.caller,
tickets.caller_phone,
tickets.contact,
tickets.contact_phone,
tickets.call_back,
tickets.location,
tickets.printable_text,
tblnotes.ntDate,
tblnotes.ntText,
tblstatus.stDesc,
tblUsers.username
FROM tblusers
RIGHT OUTER JOIN tickets ON tblusers.ID = tickets.ownerID
LEFT OUTER JOIN tblstatus ON tblstatus.stID = tickets.statusID
LEFT OUTER JOIN tblnotes ON tblnotes.ntID = tickets.noteID
WHERE tblstatus.stDesc <> "Closed"
EDIT: try this
SELECT
tickets.ticketID,
tickets.ticket,
tickets.name1,
tickets.address1,
tickets.city,
tickets.cstate,
tickets.zip,
tickets.caller_type,
tickets.phone,
tickets.caller,
tickets.caller_phone,
tickets.contact,
tickets.contact_phone,
tickets.call_back,
tickets.location,
tickets.printable_text,
tblnotes.ntDate,
tblnotes.ntText,
tblstatus.stDesc,
tblUsers.username
FROM tickets
INNER JOIN tblusers ON tblusers.ID = tickets.ownerID
INNER JOIN tblstatus ON tblstatus.stID = tickets.statusID
LEFT OUTER JOIN tblnotes ON tblnotes.ntID = tickets.noteID
WHERE tickets.statusID <> 3
posting as an answer, as I am unable to comment
You have a condition where tblstatus.stDesc <> "Closed"
assuming you have an index here on stID
change that to where tblstatus.stID <> put the id value
also change your left outer joins to inner joins, as any ways you have a where condition, you can keep the left join on tblnotes as I am not sure if it may have a row corresponding to tbltickets
i will also move the tickets table to from and then do an inner join with tblusers
use left outer join only when the join table may not have data, but you still want to show data from your main table
I have 3 tables
movies_detais
movies_revies
movies_gossips
What I want is that I want all the data whose movies_relesed_type=0 and movies_type=1
But I am not getting desired values
Code
Select md.movies_name,
md.movies_description,
mr.rt_user_comments,
mg.gossip_desc
from movies_details md
Inner join movies_reviews mr
on md.movies_id=mr.movie_review_id
Inner join movies_gossips mg
on md.movies_id=mg.movies_gossip_id
and md.movie_relesed_type='0'
and md.movie_type='1'
With this code I am only getting one row whose movies_relesed_type=0 and movies_type=1, but in my table I am having other rows also which meets the condition but they are not displaying.
I think this is a case where you want the conditions in the where clause:
Select md.movies_name,
md.movies_description,
mr.rt_user_comments,
mg.gossip_desc
from movies_details md Inner join
movies_reviews mr
on md.movies_id=mr.movie_review_id left join
movies_gossips mg
on md.movies_id = mg.movies_gossip_id
where md.movie_relesed_type='0' and md.movie_type = '1';
A left outer join returns all the rows from the first table, even when the condition in the on clause evaluates to not-true. This is true regardless of which table the conditions refer to. So, you cannot filter on the first table in the on clause using a left outer join.
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
I have this query:
SELECT
TA.id,
T.duration,
DATE_FORMAT(TA.startTime,'%H:%i') AS startTime,
TEI.displayname,
TA.threatment_id,
TE.employeeid,
TTS.appointment_date
FROM
tblEmployee AS TE
INNER Join tblEmployeeInfo AS TEI ON TEI.employeeinfoid = TE.employeeinfoid
LEFT OUTER Join tblAppointment AS TA ON TE.employeeid = TA.employee_id
LEFT OUTER Join tblThreatment AS T ON TA.threatment_id = T.threatmentid
LEFT OUTER Join tblAppointments AS TTS ON TTS.id = TA.appointments_id
AND TTS.appointment_date = '2009-10-19'
LEFT OUTER Join tblCustomerCard AS TCC ON TCC.customercardid = TTS.customercard_id
WHERE
TE.employeeid = 1
What I try to accomplish is to select an employee, if available, all appointments at a given date. When there aren't any appointments, it should at least give the information about the employee.
But right now, it just gives all appointments related to an employee, and passes null when the date doesn't match. Whats going wrong here?
Because you are doing a left OUTER join, it will only join those records that match the On condition and will attach Null when the condition is not met.
You will still get records for which there is no Appointments on that date.
If you did an INNER join, then if the On condition is not met, no record will be output. So you will not get any records for which there are no appointments on that date.
Ok, not sure what database you are on, but this would work on SQL server :
select * from tblEmployee TA
...
left join
( select * from tblAppointments ed where ed.appointment_date = '10/01/2008' ) TTS
on ON TTS.id = TA.appointments_id
Thats the vibe anyway! You might need to tinker a bit.. Im at work and cant get the whole thing going for ya! :)