2

我想要的是来自 tbmedAssign 的所有记录,其中 zoneid 或区域名称作为参数给出。如何为此编写查询?请帮忙。

表名 tablzone

zone_id(PK)   ZoneName
-----------   --------
1             east
2             west
3             north
4             south

tbluser

usrId(PK) userzoneId(FK to tblzone)   username
--------  -------------------------   ------------
1         1                           manish
2         3                           rahul
3         2                           ankit
4         4                           amir
5         2                           rashmi
6         1                           akash
tbldoctor

docId(PK)    usrId(Fk to tbluser)      docname
--------     --------------------      ------------
1             2                        hemant
2             2                        chintu
3             3                        rahim
4             1                        salman
5             3                        kishor
6             3                        saurabh
7             2                        banti

tblmedAssign 

transId(Pk)  doctorId(FK to tbldoctor)    medId(FK)    dateInsert
----------   -------------------------    ------   -----------
1            2                            2        20/12/2012
2            3                            3        21/12/2012
3            2                            3        23/12/2012
4            4                            1        24/12/2012

tblmedia

medid(PK)   medianame 
---------   ---------
1           casfung 
2           inem 
3           media1
4           tplan
5           casfung test 

我想要来自 tblmedAssign 的所有记录,其中医生属于 tbluser 中的特定用户,而在 tbluser 中,用户属于特定区域,并且区域 ID 作为参数提供?例如 zoneid= 1;

我也想在记录中选择 medianame

4

4 回答 4

3

基本上,您需要将四个表与它们的链接列连接起来。试试这个,

SELECT  a.*,
        b.*,
        c.*,        
        d.*,
        e.*
FROM    tblmedAssign a
        INNER JOIN tblDoctor b
            ON a.doctorID = b.docID
        INNER JOIN tblUser c
            ON b.usrID = c.usrID
        INNER JOIN tblZone d
            ON c.userzoneID = d.zone_ID
        INNER JOIN tblmedAssign e
            ON e.medid = a.medid
WHERE   d.zone_id = @zone_id OR    -- supply value here
        d.zoneName = @zoneName
于 2012-09-19T09:28:32.293 回答
0

通过JOIN三个表,类似于:

SELECT -- what you want to select
FROM tblmedAssign ta
LEFT JOIN tbldoctor td ON ta.doctorId = td.docId
LEFT JOIN tbluser tu ON td.usrId = tu.usrId
LEFT JOIN tablzone tz ON tu.userzoneId = tz.zone_Id
WHERE tz.zone_Id = @zoneIdParam
于 2012-09-19T09:30:38.243 回答
0
select * 
from tblmedAssign 
inner join tbldoctor on tbldoctor.docId = tblmedAssign.doctorId
inner join tbluser on tbluser.usrId = tbldoctor.usrId
inner join tablzone on tablzone.zone_id = tbluser.userzoneId
where tablzone.ZoneName = 'east' 
or tablzone.zone_id = 2
于 2012-09-19T09:37:43.717 回答
0

这应该符合您的目的

select * 
from
tblmedAssign, tbldoctor, tbluser, tablzone 
where
tblmedAssign.doctorId = tbldoctor.docId and 
tbldoctor.usrId = tbluser.usrId and 
tbluser.userzoneId =  tablzone.zone_id and 
(tablzone.zone_id = x or tablzone.ZoneName = 'y')
于 2012-09-19T10:17:32.320 回答