开发者

Using JOINS in MySQL

I have this query which works perfectly:

开发者_StackOverflow
SELECT *
FROM Customer
WHERE SacCode IN
(
    SELECT SacCode
    FROM SacCode
    WHERE ResellerCorporateID = 392
    ORDER BY SacCode
)
AND CustomerID IN
(
    SELECT CxID
    FROM CustAppointments
    WHERE AppRoomID IN
    (
        SELECT AppRoomID
        FROM ClinicRooms
        WHERE ClinID IN
        (
                SELECT ClinID
                FROM AppClinics
                WHERE ClinDate >='20090101'
                AND ClinDate <='20091119'
        )
    )
)

However, I need to see the value of ClinDate (inside the last nested query) so I've been told I need to rework the query using JOINS.

I have no idea how, can someone help please?

Thanks.


Here's a start:

SELECT     *
FROM       Customer c
INNER JOIN CustAppointments ca ON ca.CxId = c.CustomerID
INNER JOIN ClinicRooms cr ON cr.AppRoomID = ca.AppRoomID
INNER JOIN AppClinics ac ON ac.ClinID = cr.ClinID
WHERE      ap.ClinDate BETWEEN '20090101' AND '20091119'
AND        SacCode IN (SELECT sc.SacCode 
                       FROM SacCode sc 
                       WHERE sc.ResellerCorporateID = 392)

This will allow you to select columns from AppClinics.


Read this http://www.w3schools.com/Sql/sql_join.asp

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜