2015-06-30 153 views
2

我被困在查詢,我想要顯示來自不同的表和他們的橋表的數據。
表是:sql加入進一步需要幫助

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) 

回答

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 
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 
+0

非常感謝所有查詢的工​​作方式相同..但被重複數據..我也用'選擇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功能F於fb.featureid = f.featureid INNER JOIN值V ON fb.valueid = v.valueid GROUP BY pname' –

+0

結果:'PNAME獎品DS設有vlue
介質30 20 discspace 20GB 小20 15 bw 30gb' –

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) 
+0

在'內部連接fbridgeid中的拼寫錯誤爲fb',它應該是'內部連接fbridge爲fb' – Arulkumar

+0

You Are Right .. –