2016-06-08 42 views
0

我希望得到一些SQL問題的建議...如何根據MySQL 5.5.x中單獨的表中的元數據對主表進行排序?

我們有一個包含非常少的信息的主表(MySQL 5.5.x)。我們還有一個元數據表,用於存儲變量/值對並引用主表。我遇到的問題是我們需要使用JOIN檢索信息來合併兩個表,但是我們需要根據特定的元數據對輸出進行排序。以下簡單的例子將說明。

這裏的架構的超級精華版本:

CREATE TABLE fundraise (
    id  INTEGER NOT NULL, 
    charity TEXT NOT NULL, 
    PRIMARY KEY(id) 
); 

CREATE TABLE meta (
    master_id INTEGER REFERENCES fundraise(id), 
    variable TEXT NOT NULL, 
    value  TEXT NOT NULL 
); 

然後,我們輸入一些信息對所有三個慈善機構:

INSERT INTO fundraise(id, charity) VALUES 
    (1, 'save the dolphins'), 
    (2, 'feed the kids'), 
    (3, 'cloth the homeless'); 

我們還插入一些元數據:

INSERT INTO meta(master_id, variable, value) VALUES 
    (1, 'name', 'Mike'), (1, 'priority', 'high'),  (1, 'start','2016'), 
    (2, 'name', 'Barb'), (2, 'priority', 'veryhigh'), (2, 'start','2012'), 
    (3, 'name', 'Sam'), (3, 'priority', 'veryhigh'), (3, 'start','2013'); 

請注意,元數據變量'start'旨在用作所需r的排序順序擴展端口。下面是我用生成報告(未分類)的SQL語句:

SELECT f.charity, m.variable, m.value 
FROM  fundraise f 
LEFT OUTER JOIN meta m ON (f.id = m.master_id); 

我得到的輸出似乎是正確的,在大多數情況下,只是我們還沒有進行排序:

 
+--------------------+----------+----------+ 
| charity   | variable | value | 
+--------------------+----------+----------+ 
| save the dolphins | name  | Mike  | 
| save the dolphins | priority | high  | 
| save the dolphins | start | 2016  | 
| feed the kids  | name  | Barb  | 
| feed the kids  | priority | veryhigh | 
| feed the kids  | start | 2012  | 
| cloth the homeless | name  | Sam  | 
| cloth the homeless | priority | veryhigh | 
| cloth the homeless | start | 2013  | 
+--------------------+----------+----------+ 

但我真正需要的是讓它顯示排序在「開始」年份,同時保留關於特定慈善團體的所有細節。換句話說,我需要看到當年的報告順序,就像這樣:

 
+--------------------+----------+----------+ 
| charity   | variable | value | 
+--------------------+----------+----------+ 
| feed the kids  | name  | Barb  | 
| feed the kids  | priority | veryhigh | 
| feed the kids  | start | 2012  | 
| cloth the homeless | name  | Sam  | 
| cloth the homeless | priority | veryhigh | 
| cloth the homeless | start | 2013  | 
| save the dolphins | name  | Mike  | 
| save the dolphins | priority | high  | 
| save the dolphins | start | 2016  | 
+--------------------+----------+----------+ 

但是我處於虧損至於如何做到這一點...任何人有關於如何做到這一點使用任何建議SQL,完全是?!?!

請提前致謝!

p.s.我想指出我使用的實際系統要複雜得多,以上是一個相當人爲的演示來簡化問題的提問。

回答

0

試試這個。

SELECT * FROM (SELECT f.id AS id,f.charity, m.variable, m.value FROM  fundraise f RIGHT OUTER JOIN meta m ON (f.id = m.master_id) GROUP BY value HAVING (variable = 'start') ORDER BY value) as sorted_table LEFT JOIN meta m2 ON sorted_table.id = m2.master_id ORDER BY sorted_table.value 

這是我使用該查詢的結果。

MariaDB [fbb]> SELECT * FROM (SELECT f.id AS id,f.charity, m.variable, m.value FROM  fundraise f RIGHT OUTER JOIN meta m ON (f.id = m.master_id) GROUP BY value HAVING (variable = 'start') ORDER BY value) as sorted_table LEFT JOIN meta m2 ON sorted_table.id = m2.master_id ORDER BY sorted_table.value 
    -> ; 
+------+--------------------+----------+-------+-----------+----------+----------+ 
| id | charity   | variable | value | master_id | variable | value | 
+------+--------------------+----------+-------+-----------+----------+----------+ 
| 2 | feed the kids  | start | 2012 |   2 | name  | Barb  | 
| 2 | feed the kids  | start | 2012 |   2 | priority | veryhigh | 
| 2 | feed the kids  | start | 2012 |   2 | start | 2012  | 
| 3 | cloth the homeless | start | 2013 |   3 | name  | Sam  | 
| 3 | cloth the homeless | start | 2013 |   3 | priority | veryhigh | 
| 3 | cloth the homeless | start | 2013 |   3 | start | 2013  | 
| 1 | save the dolphins | start | 2016 |   1 | name  | Mike  | 
| 1 | save the dolphins | start | 2016 |   1 | priority | high  | 
| 1 | save the dolphins | start | 2016 |   1 | start | 2016  | 
+------+--------------------+----------+-------+-----------+----------+----------+ 
9 rows in set (0.01 sec) 

MariaDB [fbb]> 
+0

這是一個非常有趣的方法。我將研究它並嘗試將其應用於實際的代碼。非常感謝! – sambo770

相關問題