Hello in my system i have a search page for a student that the admin will be able to view
the students history i have a problem with showing the last name of his/her adviser which is lname_A. This is the code i currently us so far everything is ok except i cant manage to get the lname_a.
$qry_display = "SELECT
a.student_id, a.section_id, a.level, a.photo, a.address, a.father_occupation, a.father_phone, a.father_company, a.mother_occupation, a.mother_phone, a.mother_company,a.gpa,
b.fname, b.sex, b.lname, b.mname, b.birth_date, b.birth_place, b.address, b.father, b.father_degree, b.mother, b.mother_degree,
c.section_name, d.adviser_id , d.lname_a
FROM tbl_er AS a
LEFT OUTER JOIN tbl_enroll AS b ON a.student_id = b.student_id
LEFT OUTER JOIN tbl_section AS c ON a.section_id = c.section_id
LEFT OUTER JOIN tbl_adviser AS d ON a.section_id = d.adviser_id
WHERE a.student_id=".$id." AND a.level='Grade 2'";
Would gladly appreciate any help.
Are you sure you are joining both tables or correct columns?
a.section_id = d.adviser_id
If every student has adviser then you should use inner join rather than left outer join.
When you use left outer join there is chance lname_a is empty when the student doesn't has adviser.
Related
I know on MySQL we can join the same table twice by giving each table its own alias, but that doesn't seem to be working on Access.
For example:
SELECT d.departmentID, d.depName, d.location, c1.memberID, c1.fullName, c1.reportsTo, c2.fullName
FROM Departments as d
INNER JOIN Contacts as c1
ON c1.departmentID = d.departmentID
INNER JOIN Contacts as c2
ON c1.reprtsTo = c2.memberID
Doing that gives me a syntax error. Does anyone know how I can join the same table (Contacts) to get the name of the person the member reports to (c2.fullName)?
Update, the Error I'm getting:
Syntax error (missing operator) in query expression 'c1.departmentID = d.departmentID INNER JOIN Contacts as c2 ON c1.reportsTo = c2.memberI'.
In MS Access, more than one JOIN requires parentheses pairings:
SELECT d.departmentID, d.depName, d.location, c1.memberID,
c1.fullName, c1.reportsTo, c2.fullName
FROM (Contacts as c1
INNER JOIN Departments as d
ON c1.departmentID = d.departmentID)
INNER JOIN Contacts as c2
ON c1.reprtsTo = c2.memberID
In such cases it's easier to let the Access design editor take care of the joins and the aliases.
The code below is based on your code but created by the design editor:
SELECT
Departments.departmentID, Departments.depName, Departments.location,
Contacts.memberID, Contacts.fullName, Contacts_1.reportsTo, Contacts_1.fullName
FROM (
Departments INNER JOIN Contacts ON Departments.departmentID = Contacts.departmentID
) INNER JOIN Contacts AS Contacts_1 ON Contacts.reportsTo = Contacts_1.memberID;
just drag the table in to the visual editor twice. It automatically renames the second instance of the table as "_1".
SELECT Contacts.EmpID, Contacts_1.EmpID AS reportsTo
FROM Contacts INNER JOIN Contacts AS Contacts_1 ON Contacts.SupervisorID=
Contacts_1.EmpID;
i was trying to apply joins on 4 tables. but i could not find proper result for that.
i have 4 tables like 1.students,2.college,3.locations,4.departments. so i have same column sid in all tables which can be used to join conditions.
i want all matched rows from four tables as mentioned columns in select statement below and unmatched rows in left table which is left outer join work.
i have tried this syntax.
select
students.sname,
college.cname,
locations.loc,
department.dept
from students, college, locaions, departments
where student.sid=college.sid(+)
and college.sid=locations.sid(+)
and locations.sid=department.sid(+);
is this right ?
This is an old-fashioned way of outer-joining in an Oracle database. For Oracle this statement is correct; in other DBMS it is invalid.
Anyway, nowadays (as of Oracle 9i; in other DBMS much longer) you should use standard SQL joins instead.
select
s.sname,
c.cname,
l.loc,
d.dept
from students s
left outer join college c on c.sid = s.sid
left outer join locations l on l.sid = c.sid
left outer join departments d on d.sid = l.sid;
Given what you've shown it would appear that what you really want is
select s.sname,
c.cname,
l.loc,
d.dept
from students s
LEFT OUTER JOIN college c
ON c.SID = s.SID
LEFT OUTER JOIN locations l
ON l.SID = s.SID
LEFT OUTER JOIN departments d
ON d.SID = s.SID
The issue in your original query is that because an OUTER JOIN is an optional join, you can end up with NULL values being returned in one of the join fields, which then prevents any of the downstream values being joined. I agree with #ThorstenKettner who observes in a comment that SID is apparently a "student ID", but it's not reasonable or appropriate to have a "student ID" field on tables named COLLEGE, LOCATIONS, or DEPARTMENTS. Perhaps you need to update your design to allow any number of students to be associated with one of these entities, perhaps using a "join" table.
Best of luck.
Sorry for Title, don't know how to explain.
Ok so I want to see if any protocol (PTC_ID) is linked to an Audit (AUD_ID), in the picture you can see there is 3 tables and each one has a value of the other.
I though of using inner join all 3 tables with the ON , ON ADA_PTCID = PTC_ID etc. and if a audit is linked with a PTC then display year?
Select AUD_YEAR
From AUD_Table at
Inner Join ADA_TABLE ad
ON at.AUD_ID = ad.ADA_AUD_ID
Inner Join PTC_TABLE pt
ON pt.PTC_ID=ad.ADA_PTCID
try
select
ptc.ptc_name,
aud.aud_year
from
ptc_table ptc
inner join
ada_table ada
on
ada.ada_ptcid=ptc.ptc_id
inner join
aud_table aud
on
aud.aud_id=ada.ada_aud_id
Something like this?
select
aud.year,ptc.name
from ada
inner join aud on ada.aud_id = aud.aud_id
inner join ptc on ada.ptc_id = ptc.ptc_id
I have a query (ContactFormTypesRequired) that returns ContactID and FormTypeID utilizing related tables that are not shown below. This is a list of FormTypes that each Contact should have related to it as a Form.
I need a query that returns Contacts that do not have one or more related forms of the FormTypes specified in the above query.
I've tried a left outer join from Form to ContactsFormTypesRequired on FormTypeID, but the results don't take into account FormTypes that each specific Contact should have.
Please let me know if you have any questions.
Thank you in advance for any suggestions.
I am writing the query this way. This first starts with your query to get needed forms as a CTE, then cross joins them to Contacts to get every needed combination, before left joining to the actual forms.
with NeededForms (<yourqueryhere>)
select distinct c.*
from Contact c cross join
NeededForms nf left outer join
Form F
on nf.FormTypeId = f.FormTypeId left outer join
ContactForm cf
on c.ContactId = cf.ContactId and
f.FormId = cf.FormId
where cf.FormId is null
I'm doing it this way, so you can answer the query of what forms are missing with a very similar query:
with NeededForms (<yourqueryhere>)
select c.*, nf.FormTypeId
from Contact c cross join
NeededForms nf left outer join
Form F
on nf.FormTypeId = f.FormTypeId left outer join
ContactForm cf
on c.ContactId = cf.ContactId and
f.FormId = cf.FormId
where cf.FormId is null
Try this simple query using NOT IN Cluase:
SELECT * FROM Contact
WHERE ContactID IN
(SELECT ContactID FROM ContactForm
INNER JOIN FORM ON ContactForm.FormID=Form.FormID
WHERE FormTypeID=#FormTypeID)
I created ContactFormTypeExist:
SELECT DISTINCT Contact.ContactID, Form.FormTypeID
FROM Contact
INNER JOIN ContactForm
ON Contact.ContactID = ContactForm.ContactID
INNER JOIN Form
ON ContactForm.FormID = Form.FormID
Then joined ContactFormTypesRequired described in the question above to ContactFormTypeExist with outer join to give me the ConactIDs that are missing related FormTypeIDs:
SELECT ContactFormTypesRequired.ConactID
FROM ContactFormTypeExist
RIGHT JOIN ContactFormTypesRequired
ON (ContactFormTypeExist.FormTypeID = ContactFormTypesRequired.FormTypeID)
AND (ContactFormTypeExist.ConactID = ContactFormTypesRequired.ConactID)
WHERE (((ContactFormTypeExist.ConactID) Is Null));
This returns all the ContactID for Contacts missing FormTypes required by their ContactType.
SELECT dbo.Monitor_Request.WorkDesc
, dbo.Monitor_Request.Request_ID
, dbo.Monitor_Request.Due_Dt
, dbo.Monitor_Request.Attempts
, dbo.Monitor_Request.Status_Ind
, dbo.Monitor_Request.Create_Dt
, dbo.Monitor_Request.Monitor_ID
, dbo.Monitor_Request.ByCustomer_ID
, dbo.Monitor_Request.ByCompanyID
, dbo.CompanyShim.Company_Name
, dbo.PostalAddressShim.HouseName
, dbo.PostalAddressShim.Street
, dbo.PostalAddressShim.Town
, dbo.PostalAddressShim.City
, dbo.PostalAddressShim.County
, dbo.PostalAddressShim.Postcode
FROM dbo.PostalAddressShim
RIGHT OUTER JOIN dbo.CompanyShim ON dbo.PostalAddressShim.Address_ID = dbo.CompanyShim.Company_Address_ID
RIGHT OUTER JOIN dbo.CUSTOMER ON dbo.PostalAddressShim.Address_ID = dbo.CUSTOMER.Address_ID
RIGHT OUTER JOIN dbo.Monitor_Request ON dbo.CUSTOMER.Customer_ID = dbo.Monitor_Request.ByCustomer_ID
AND dbo.CompanyShim.Company_ID = dbo.Monitor_Request.ByCompanyID
I have created a view to display customer details with their addresses. A customer can be an individual or a company. They are stored in in different tables. For individuals the details are stored in a table called Customer and the company details are stored in the Company table. I am trying to create a view of all the customers in the database to display their addresses. When I join the tables separately I am getting the address details but if I am joining both the tables(Customer and Company) at once I am not getting null values for address details columns.
How can I get all the address details of the customers (Individual or company) from the database
I rarely found a need to use RIGHT OUTER JOIN and every time I encounter it, it forces me to think from right to left complicating things.
If I didn't goof up to badly, following joins should be equivalent to what you have written
SQL Statement
dbo.Monitor_Request mr
LEFT OUTER JOIN dbo.Customer c ON c.Customer_ID = mr.ByCustomerID
LEFT OUTER JOIN dbo.CompanyShim cs ON cs.Company_ID = mr.ByCompanyID
LEFT OUTER JOIN dbo.PostalAddressShim pas ON pas.Address_ID = c.Address_ID
AND pas.Address_ID = cs.Company_Address_ID
Now this is something I can read and theorize about. What's immediate obvious from this statement is the AND clause in joining the adresses with customers and companies effectively negating each other and returning no adresses at all.
My guess is you should simply
replace your RIGHT JOINS with LEFT JOINS
use OR instead of AND
SQL Statement
dbo.Monitor_Request mr
LEFT OUTER JOIN dbo.Customer c ON c.Customer_ID = mr.ByCustomerID
LEFT OUTER JOIN dbo.CompanyShim cs ON cs.Company_ID = mr.ByCompanyID
LEFT OUTER JOIN dbo.PostalAddressShim pas ON pas.Address_ID = c.Address_ID
OR pas.Address_ID = cs.Company_Address_ID
At first glance, try changing your RIGHT OUTER JOINs to LEFT OUTER JOINs.