2016-06-08 13 views
0

Ich hoffe, einige Tipps zu einem SQL-Problem zu bekommen ...Wie sortiere ich eine Master-Tabelle basierend auf Metadaten in einer separaten Tabelle in MySQL 5.5.x?

Wir haben eine Master-Tabelle (MySQL 5.5.x), die sehr wenig Informationen enthält. Wir haben auch eine Metadatentabelle, die Variablen/Wert-Paare speichert und auf die Master-Tabelle verweist. Das Problem, das ich habe, ist, dass wir die Informationen mithilfe eines JOIN abrufen müssen, um beide Tabellen zu kombinieren, aber wir müssen die Ausgabe basierend auf einem bestimmten Meta-Datum sortieren. Das folgende triviale Beispiel wird veranschaulichen.

Hier ist eine super-destillierte Version des Schemas:

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 
); 

Wir haben dann einige Informationen für alle drei gemeinnützige Organisationen ein:

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

Wir haben auch einige Metadaten einfügen:

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'); 

Beachten Sie, dass die Metadatenvariable 'start' als Sortierreihenfolge für das erforderliche r verwendet werden soll E-Mail. Hier ist die SQL-Anweisung Ich bin mit dem Bericht (unsortiert) zu erzeugen:

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

Der Ausgang Ich bin immer scheint richtig, zum größten Teil, mit der Ausnahme, dass wir noch nicht sortiert haben:

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

Aber was ich wirklich brauche ist, dass es sortiert nach dem "Start" Jahr angezeigt wird, während alle Details über eine bestimmte Wohltätigkeitsorganisation zusammen gespeichert werden. Mit anderen Worten, ich brauche den Bericht nach dem Jahr, um zu sehen, wie folgt aus:

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

Aber ich bin ratlos, wie dies zu tun ... hat jemand irgendwelche Vorschläge, wie dies zu tun mit SQL, ausschließlich?!?!

Mai danke im voraus!

p.s., möchte ich darauf hinweisen, dass das tatsächliche System, das ich verwende, viel viel komplexer ist, und das oben genannte ist eine eher erfundene Demo, um das Stellen der Frage zu vereinfachen.

Antwort

0

Versuchen Sie es.

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 

Dies ist mein Ergebnis mit dieser Abfrage.

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

das ist ein sehr interessanter Ansatz. Ich werde es studieren und versuchen, es auf den tatsächlichen Code anzuwenden. Danke vielmals! – sambo770