sql join需要进一步帮助

时间:2015-06-30 08:08:41

标签: php mysql sql join

我被困在一个查询中,我想显示来自不同表及其桥表的数据 表格是:

bridge (bid,planid,fbridgeid)
fbridge (fbridgeid,featureid,valueid)
features (featureid,fname)
value (valueid,value)
plans (planid,name,price,discount)

以下是我的查询:

SELECT 
(NAME AS pname, price AS prize,
 discount AS ds, fname AS feature, VALUE AS vlue 
FROM plans  )

我想从数据库中获取所有列,但是根据计划

like planid 1 ->> contains ->> feature id 1,2,3 and valueid 1,3,5 etc )

3 个答案:

答案 0 :(得分:1)

Join将是:

SELECT pl.name AS pname, pl.price AS prize , pl.discount AS ds , fe.fname AS feature, va.VALUE AS vlue
FROM plans pl
JOIN bridge br ON br.planid = pl.planid
JOIN fbridge fb ON fb.fbridgeid = br.fbridgeid
JOIN features fe ON fe.featureid = fb.featureid
JOIN value va ON va.valueid = fb.valueid
WHERE pl.plainid = 1

答案 1 :(得分:0)

将表格加入正确的字段。

SELECT *
FROM plans p
INNER JOIN bridge b ON p.planid = b.planid
INNER JOIN fbridge fb ON b.fbridgeid = fb.fbridgeid
INNER JOIN features f ON fb.featureid = f.featureid
INNER JOIN value v ON fb.valueid = v.valueid

然后您可以从中选择所有字段。

SELECT p.name AS pname, p.price AS prize, p.discount AS ds, f.fname AS feature, v.value AS vlue
FROM plans p
INNER JOIN bridge b ON p.planid = b.planid
INNER JOIN fbridge fb ON b.fbridgeid = fb.fbridgeid
INNER JOIN features f ON fb.featureid = f.featureid
INNER JOIN value v ON fb.valueid = v.valueid

答案 2 :(得分:0)

select b.bid,b.planid,b.fbridgeid,fb.fbridgeid,f.featureid,f.fname,v.valueid,v.value,p.name,p.price,p.discount from bridge as b 
inner join fbridge as fb on fb.bridgeid=b.bridgeid 
inner join features as f on f.featureId=fb.featureid 
inner join value as v on v.valueid=fb.valueid 
inner join plans as p on p.planid=b.planid 
where p.planid=1 and f.featureId in (1,2,3) and v.valueid in (1,3,5)