How to write this SQL Statement to join 3 tables - mysql

I have 3 tables:-
Register(Stu_id, name, address,....,status),
Applicant_Choice(Stu_id,Sub_id1,Sub_id2,Sub_id3) for 3 selected subjects
Subjects(Sub_id,Subject_name,....) for subject names
I want to extract report in the form :- id,name, address,... 3 subject names for the Sub_id1,Sub_id2,Sub_id3 present in Applicant table where Register.status = 6.
So far I am using loops to generate the subject names for each record. But There will be 1000 records in Applicant table. So in that case so much queries to generate a pdf file will be too much!!! Can anybody show me how to write this query? Thanks in advance.

Try this one out:
select r.stu_id, r.name, r.address, s1.subject_name, s2.subject_name, s3.subject_name
from register r
inner join applicant_choice a
on r.stu_id = a.stu_id
inner join subjects s1
on a.sub_id1 = s1.sub_id
inner join subjects s2
on a.sub_id2 = s2.sub_id
inner join subjects s3
on a.sub_id3 = s3.sub_id
where r.status = 6;
Example fiddle: http://sqlfiddle.com/#!9/5daaa/3

Related

Retrieve data from multiple table in sql using some condition

I have created three tables namely
studentdetails (sid,sname,slname),
books (bid,bname,bprice),
bookingdetails (sid,bid)
sql query
I want to display the student name who has booked dbms book.
JOINS should do the work, like the query below:
SELECT studentdetails.name from books
inner join bookingdetails on bookingdetails.bid = books.bid
inner join studentdetails on studentdetails.id = bookingdetails.sid
where books.name = 'dbms'
Join help us to fetch data from multiple table,so below query can work :
SELECT Slname
FROM studentdetails sd
JOIN BookingDetails bd
ON sd.sid = bd.sid
JOIN books bk
ON bk.bid = bd.bid
AND bk.bname = 'DBMS'

Select rows based on an array of values and join data

I have a MySQL database of contacts with three tables.
person
personContact
personDetails
Each contact shares a primary key called 'ID'
The personContact table contains a value called 'personZip' which happens to be their mailing address zip code.
I'd like to write a SQL query that will give me all the contact data for each person in a specific array of zip codes.
I've written a simple statement to perform an inner join on 2 of the tables:
SELECT * FROM `personContact`
INNER JOIN person
ON personContact.ID=person.ID
I've written a statement to select only the zip codes I need:
SELECT * FROM 'personContact'
WHERE personContact.personZip=12564
OR personContact.personZip=12563
OR personContact.personZip=12522
OR personContact.personZip=12590
OR personContact.personZip=12594
OR personContact.personZip=12533
OR personContact.personZip=12570
OR personContact.personZip=12589
OR personContact.personZip=10509
I'm not sure how to perform two joins, to merge all columns from all three tables.
I'm not sure how to write the query to accommodate both the selection of zip codes and the JOINS.
MySQL errors are not helping me move in the right direction.
You were almost there. Use in which is equivalent to or.
SELECT pc.* --select columns from the other tables as needed.
FROM
`personContact` pc
INNER JOIN person p ON pc.ID = p.ID
INNER JOIN personDetails pd on pd.ID = p.ID
where pc.personzip in (12563, 12522, 10509) -- add more zips as needed
Join all three tables and return all columns while filtering by zip code:
SELECT person.*, personContact.*, personDetails.*
FROM person
INNER JOIN personContact ON personContact.ID = person.ID
INNER JOIN personDetails ON personDetails.ID = person.ID
WHERE personContact.personZip = 12564
OR personContact.personZip = 12563
OR personContact.personZip = 12522
OR personContact.personZip = 12590
OR personContact.personZip = 12594
OR personContact.personZip = 12533
OR personContact.personZip = 12570
OR personContact.personZip = 12589
OR personContact.personZip = 10509
EDIT: Multiple OR statements can be replaced using IN as others have suggested
WHERE personContact.personZip IN (12564, 12563, 12522, 12590 ...)

How to search where some data is in table 1 and other data is in table 3 with both having a UUID the same

I have 3 tables
For instance
Salestable
-ID
-variableB
-customerUUID
-variableC
Customertable
-customerUUID
-contractUUID
Contracttable
-contractUUID
-variableD
So I am currently doing a SQL Query on salestable
Like:
SELECT DISTINCT variableB FROM Salestable WHERE variableD = "blah";
How can I do this? Where I can find the contract associated with the current salestable?
A bit more info
They are all a 1:1 relationship - so Contracttable is tied to 1 Customertable which is tied to 1 salestable
There is a LOT of data in my database thousands of entries - this query does not run constantly but does need to run somewhat efficent.
SELECT a.*
FROM SalesTable a
INNER JOIN CustomerTable b
ON a.customerUUID = b.customerUUID
INNER JOIN Contracttable c
ON b.contractUUID = c.contractUUID
WHERE c.variableD = 'valueHere'
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
If you sure the contract exists use this (othewise swap INNER FOR LEFT):
SELECT variableB, variableD
FROM Salestable t1
INNER JOIN Customertable t2 ON (t1.customerUUID = t2.customerUUID)
INNER JOIN Contracttable t3 ON (t3.contractUUID = t2.contractUUID)
WHERE variableD = "blah"
GROUP BY t1.variableB

MySQL Left Join Not Working

I am trying to just select data from table agency where that agency has entry in the commission table with a certain carrier. My SQL query is returning all entries and 3 times. What am I doing wrong.
$allquery = mysql_query("SELECT agency.ID, agency.agencyname, agency.contdate, agency.physcity FROM agency LEFT JOIN commission ON commission.repnum = agency.repid WHERE agency.repid = '$repid' AND commission.repnum = '$repid' AND commission.carrier = 'Carrier' ")or die(mysql_error())
You are not joining two tables unless you actually specify a join criteria involving columns from each table.
SELECT agency.ID, agency.agencyname, agency.contdate, agency.physcity
FROM agency LEFT JOIN commission ON commision.repid = agency.repid AND commission.carrier = 'Carrier'
Q: Why don't you use an inner join if you want to refer to elements in "commission"?
Q: Why are you doing a join at all??? I don't see how "commission" is linked to "agency". Does "commission" have a "repid" field, too?
Anyway, based on what you've shown, I'd recommend this:
SELECT ID, agencyname, contdate, physcity
FROM agency
where repid = '$repid'

How to concatenate the output of a SQL query into one string

I am newbie in writing SQL queries and this is for a mySQL database.
I have a Table called PatientVisit (PV), which has a one to one with BillMaster (BM). Each visit has one bill, which then has a one to many with BillDetail (BD). When i list out the Visit details from PatientVisit (PV), i need to print a string with the set of 'ServiceName' columns associated with that one visit.
So for example, the PatientVisit.ID number '1' has a corresponding BillMaster.Bill No '1' which has 2 entries in BillDetail 'Consultation' and 'Dressing'.
When i print details of Visit 1, i need 'Consultation,Dressing' as one string value for the 'Service Name' column.
If i had a one to one , then the query would have been simple as follow :
select PV.ID, BM.BillNo, BD.ServiceName
FROM PatientVisits PV INNER JOIN BillMaster BM ON BM.VisitID = PV.ID
INNER JOIN BillDetail BD ON BD.BillNo = BM.BillNo
WHERE ....
However, since it is one to many for the ServiceName column, how can this query be written ?
Thanks,
Chak.
Try this
select PV.ID, BM.BillNo,
GROUP_CONCAT(BD.ServiceName)
FROM PatientVisits PV INNER JOIN BillMaster BM ON BM.VisitID = PV.ID
INNER JOIN BillDetail BD ON BD.BillNo = BM.BillNo
WHERE ..
GROUP BY PV.ID,BM.BillNo
..