我希望得到一些有关 SQL 问题的建议...
我们有一个主表(MySQL 5.5.x),其中包含的信息很少。我们还有一个元数据表,用于存储变量/值对并引用主表。我遇到的问题是,我们需要使用 JOIN 来组合两个表来检索信息,但我们需要根据特定的元数据对输出进行排序。下面的小例子将说明这一点。
这是该架构的 super 精简版本:
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 | +--------------------+----------+----------+
但我真正需要的是它在“开始”年显示排序,同时将有关特定慈善机构的所有详细信息保留在一起。换句话说,我需要按年份查看报告顺序,如下所示:
+--------------------+----------+----------+ | 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]>
关于mysql - 如何根据 MySQL 5.5.x 中单独表中的元数据对主表进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37692899/