sql连接需要进一步帮助

Posted

技术标签:

【中文标题】sql连接需要进一步帮助【英文标题】:sql joins needed help further 【发布时间】:2015-06-30 08:08:40 【问题描述】:

我遇到了一个查询,我想显示来自不同表及其桥接表的数据。 这些表格是:

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 )

【问题讨论】:

【参考方案1】:

Joins 将是:

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

【讨论】:

非常感谢所有的查询都是一样的..但是数据是重复的..我也使用了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 GROUP BY pname的group by 结果:pname prize ds feature vlue <br/> medium 30 20 discspace 20gb small 20 15 bw 30gb 【参考方案2】:

在正确的字段中加入您的表格。

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

【讨论】:

【参考方案3】:
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)

【讨论】:

inner join fbridgeid as fb中的错字,应该是inner join fbridge as fb

以上是关于sql连接需要进一步帮助的主要内容,如果未能解决你的问题,请参考以下文章

需要帮助从 sql 内连接获取 SUM 数据

需要帮助连接 SQL

需要帮助优化外连接 SQL 查询

我需要帮助格式化 SQL 查询中的内部连接命令

需要有关 SQL 中复杂 Join 语句的帮助

需要帮助来优化 ORACLE SQL 查询 [关闭]