2015-09-08 70 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