For these tables here (circled)
http://imgur.com/Q1HlJ
What would the best join be to use for them , I tried using OUTER join but it would not return any rows even if there was matching data.
Thanks
QUERY
select *
from
hr
OUTER JOIN
hr
ON
hr.procedure_id = procedure.procedure_id
OUTER JOIN
staff
ON
staff.staff_id = hr.staff_id
Where hr.procedure_id = procedure.procedure_id
I would suggest reading up on JOINs. It is hard to know what you are looking for. We won't make that decision for you.
What you probably want is this:
select p.*, hr.*, s.*
from procedure p
left outer join hr on p.procedure_id = hr.procedure_id
left outer join staff s on hr.staff_id = s.staff_id
That will give you results unless there are no rows in procedure.
There are different ways that you could put these together, though, depending on what exactly you want. I don't know what "hr" stands for here, so I can't use common sense to figure that out.
Don't bother with JOIN at all, and let the query optimizer handle it for you.
select * from hr, procedure, staff
where hr.procedure_id = procedure.procedure_id and staff.staff_id = hr.staff_id
Related
I'm trying to study SQL.
I have a problem with JOIN
I want to display ref_id, pro_name, class_name but I couldn't.
I find EFFICIENT solution.
MY QUERY (DOESN'T WORK)
SELECT
ref_id, pro_name, class_name
FROM
RC, RP, PP, LP
WHERE
RC.ref_id = RP.ref_id
Avoid using commas be CROSS JOIN
You could use JOIN to instead of commas
like this.
SELECT
RP.ref_id, PP.pro_name, LP.class_name
FROM
RP
LEFT JOIN RC ON RC.ref_id = RP.ref_id
LEFT JOIN PP ON PP.pro_id = RP.pro_id
LEFT JOIN LP ON LP.lec_id = RP.lec_id
Never use commas in the FROM clause. Always use proper, explicit, standard JOIN syntax.
You would seem to want:
select rp.pro_id, pp.pro_name, lp.class_name
from rp left join
pp
on rp.pro_id = pp.pro_id left join
lp
on rp.lec_id = lp.lec_id;
Note the use of left join. This ensure that all rows are in the result set, even when one or the other joins doesn't find a matching record.
From what I can see, the table rc is not needed to answer this specific question.
This is my structure of tables:
tables_structure
and there's tbl_owner, tbl_rooms and tbl_class.
How do I get id_class and class name from tbl_class.
The original source code is like this:
SELECT
clas.id_class,
clas.class_name
FROM
tbl_rooms AS room
LEFT JOIN tbl_class AS clas ON room.id_class = clas.id_class
LEFT JOIN tbl_owner AS own ON room.id_owner = own.id_owner
WHERE own.id_owner='1';
However, it did not work. Please help me?
SELECT
clas.id_class,
clas.class_name
FROM
tbl_rooms AS room
LEFT JOIN tbl_class AS clas ON room.id_class = clas.id_class
LEFT JOIN tbl_owner AS own ON room.id_owner = own.id_owner
WHERE own.id_owner='1';
Left Joins and wheres can be a little confusing.
Here you are asking for all rooms and if they have a class return it, it they have an owner return it. Which is done by the left join.
Then after getting that info, Your asking to filter the results by owner = 1
Given that there were left joins used to build this data, you have now said only show the results where both joins matched. Effectively turning the left join to an inner join.
I would take away the where clause and check the join conditions are producing results you expect first, then start to allow the where to narrow the result set.
The correct way to check if a left join is a condition is (own.id_owner='1') OR (own.id_owner is null)
That will bring back all owner = 1 and all unknown owners
I'm having some trouble trying to get all the data out of this database in one query.
Here is the Database scheme
I know that there is a way to do this with a join but i'm not sure how.
I need all the data from Questionnaire, Booking, and Customers.
I think i have to join on the Booking has customers but i'm not sure.
At the moment i have something like this
SELECT *
FROM Booking,Questionaires,Customers
WHERE accepted = 0
JOIN ON Booking_idBooking = Customers_idCustomers
Can anyone help me out?
Thanks beforehand!
You could use inner join this way
SELECT * FROM Booking_idBooking as BB
INNER JOIN Booking as B on BB.Booking_idBooking = B.idBooking
INNER JOIN Customers as C on BB.Customers_idCustomers = C.idCustomers
INNER JOIN Questionaires as Q on Q.idQuestionaires = B.uestionaires_idQuestionaires
WHERE B.accepted = 0 ;
I would go about doing something like this, but you may need left joins depending one your circumstances.
select
*
from
customers c
inner join booking_has_customers bhs on
bhs.Customers_idCustomers = c.idCustomers
inner join booking b on
b.idBooking = booking_idBooking
inner join Questionaires q on
q.idQuestionaires = b.Questionairs_idQuestionairs
where
b.accepted = 0;
Just a note, you should really change your naming conventions.
For example booking is singular, yet customers is plural in your schema. They should be consistent. Also, something like idBooking and booking_idBooking is not great - maybe use id and booking_id. Anyway just suggestions.
You also might want to invest some time in learning how to write joins. Checkout w3schools for good examples.
SELECT b.*,q.*,c.*
FROM Booking AS b
JOIN Questionaries AS q ON q.idQuestionaries=b.Questionaries_idQuestionaries
JOIN Booking_has_Customers AS bhc ON bhc.Booking_idBooking=b.idBooking
JOIN Customers AS c ON c.idCustomers=bhs.Customers_idCustomers
WHERE b.accepted=0
But come on, change ID names into simple id and you'll have:
SELECT b.*,q.*,c.*
FROM Booking AS b
JOIN Questionaries AS q ON q.id=b.id
JOIN Booking_has_Customers AS bhc ON bhc.id=b.id
JOIN Customers AS c ON c.id=bhs.id
WHERE b.accepted=0
I would like to know whether there's any difference between the queries below. More specifically, I am interested in the joins and what's the preferred way of inner joining '=' or the clause inner join. Thanks
select distinct gsm.mobile_no, par.name par
from ccare.customer cus, ccare.cu_partner par, service.contract contr, service.gsm gsm
where par.code = cus.partner_code
and contr.contract_no = gsm.contract_code
and gsm.code = sgcp.code
select distinct gsm.mobile_no, par.name par
from ccare.customer cus
inner join ccare.cu_partner par on par.code = cus.partner_code
inner join service.contract contr on contr.contract_no = service.gsm.contract_code
inner join charge.gsm_charge_plan sgcp on sgcp.service_code = gsm.code
These are just 2 different ways of joins.
First one is called THETA style of join while another one is known as ANSI style of join. Both are similar and it is up to you which method you choose.
Hi all champions out there
I am far from a guru when it comes to high performance SQL queries and and wonder in anyone can help me improve the query below. It works but takes far too long, especially since I can have 100 or so entries in the IN () part.
The code is as follows, hope you can figure out the schema enough to help.
SELECT inv.amount
FROM invoice inv
WHERE inv.invoiceID IN (
SELECT childInvoiceID
FROM invoiceRelation ir
LEFT JOIN Payment pay ON pay.invoiceID = ir.parentInvoiceID
WHERE pay.paymentID IN ( 125886, 119293, 123497 ) )
Restructure your query to use a join instead of a subselect. Also, use an INNER JOIN instead of a LEFT JOIN to the Payment table. This is justified, since you have a WHERE filter that would filter rows without a match in the Payment table anyway.
SELECT inv.amount
FROM invoice inv
INNER JOIN invoiceRelation ir ON inv.incoiceID = ir.childInvoiceID
INNER JOIN Payment pay on pay.invoiceID = ir.parentInvoiceID
WHERE pay.paymentID IN (...)
One way to improve performance is to have a good index on relevant columns. In your example, an index on inv.invoiceID would probably speed up the query quite a bit.
Also on pay.paymentID
Try this and see if it helps:
ALTER TABLE invoice ADD INDEX invoiceID_idx (invoiceID);
and
ALTER TABLE Payment ADD INDEX paymendID_idx (paymentID);
I think instead of first in you can use inner join.
select inv.amount from invoice inv
inner join invoiceRelation ir on (inv.invoiceID = ir.childInvoiceID)
LEFT JOIN Payment pay ON pay.invoiceID = ir.parentInvoiceID
WHERE pay.paymentID IN (125886,119293,123497)
What if you try like this instead:
SELECT inv.amount
FROM invoice inv
inner join invoiceRelation ir on inv.invoiceID = ir.parentInvoiceID
left join Payment pay on pay.invoiceID = ir.parentInvoiceID
WHERE pay.paymentID IN ( 125886, 119293, 123497 )
(OR) better; if you sure you have a invoiceID FK in payment table then make that left join to a inner join.
In a nutshell, you should try avoiding correlated subquery at all times if you can replace that subquery to a join.