2015-09-08 49 views
1

的某些属性值我有一些数据如下表:SQl的排序基于连接表

Collector: 
dcid name hostid userid 
123 test host1 234 
567 hello host2 345 

CollectorConfiguration: 
ID propertyname propertyvalue collector_id(foreign key) 
id1 c_source  local   123 
id2 c_createdby admin   123 
id3 c_pattern  JBoss   123 
id4 c_source  remote   567 
id4 c_createdby admin   567 
id4 c_pattern  Apache   567 

现在我需要从收集表中的所有记录在CollectorConfiguration排序的列值“c_pattern”表。

我试图使用内部连接写查询,但我无法得到所需的结果。请帮忙。

注意:返回的结果仅包含收集器表的列,即它应该像收集器中的select *那样工作,但对c_pattern属性值具有sortinn。

Desired output(with ascending order on c_pattern): 
567 hello host2 345 
123 test host1 234 
+0

你应该提供你的愿望OUTP UT。同时告诉我们你有什么尝试。 –

+0

'propety name'是什么意思c_pattern“'?您在不是该列的值的列上进行排序,除非您的意思是按该值过滤。 –

+0

@JuanCarlosOropeza我编辑了相应的问题。让我知道你是否需要任何其他信息。 – geekprogrammer

回答

1

随着EAV模型,你应该有一堆帮助视图来克服这样的问题。该意见将作为表,如collector_patterns,collector_sources等

SELECT c.* 
FROM Collector c LEFT JOIN 
CollectorConfiguration cc on c.dcid = cc.collector_id 
where cc.propertyname = 'c_pattern' 
ORDER BY cc.propertyvalue DESC 

因此,为了从该查询视图你会写这样的:

CREATE VIEW collector_pattern AS 
SELECT c.*, cc.propertyvalue AS pattern 
FROM Collector c LEFT JOIN 
CollectorConfiguration cc on c.dcid = cc.collector_id 
where cc.propertyname = 'c_pattern' 
1
SELECT a.* FROM Collector a 
LEFT JOIN CollectorConfiguration b ON b.collector_id=a.dcid 
WHERE b.propertyname="c_pattern" 

而这个问题不是那么清楚,我,但我猜你正在寻找这样。

+0

感谢您的回答。你的答案几乎奏效,只有我需要在你的查询中添加order by子句。 – geekprogrammer

1

还是关于不明确模式,但看起来应该是这样

SQL FIDDLE DEMO

SELECT c.[dcid], c.[name], [hostid], [userid] 
FROM Collector c 
LEFT JOIN CollectorConfiguration cc 
    ON cc.collector_id=c.dcid 
WHERE cc.propertyname = 'c_pattern' 
+0

你的答案几乎可行,我只需要在查询中提及顺序,非常感谢你花时间在此。 – geekprogrammer