我如何根据 MySQL 中单独 table 中的元数据对母版 table 进行排序 5.5.x?

How do I sort a master table based on metadata in a separate table in MySQL 5.5.x?

我希望得到一些关于 SQL 问题的建议...

我们有一个大师table (MySQL 5.5.x),包含的信息很少。我们还有一个元数据 table,它存储 variable/value 对并引用主 table。我遇到的问题是我们需要使用 JOIN 检索信息以组合两个 tables,但我们需要根据特定的元数据对输出进行排序。下面的小例子将说明。

这是架构的超级提炼版本:

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' 旨在用作所需报告的排序顺序。这是我用来生成报告的 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     |
+--------------------+----------+----------+

但我真正需要的是它按 "start" 年排序显示,同时将特定慈善机构的所有详细信息放在一起。换句话说,我需要按年份查看报告顺序,如下所示:

+--------------------+----------+----------+
| 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.,我想指出我使用的实际系统要复杂得多,上面是一个相当人为的演示,以简化问题的提出。

试试这个。

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