请我有以下表格
Field
+----+--------+----------+---------------------+
| id | name | Type | date |
+----+--------+----------+---------------------+
| 1 | price | int | 2009-09-02 00:07:07 |
| 2 | width | float | 2009-09-02 00:07:08 |
| 3 | height| float | 2009-09-02 00:07:30 |
+----+--------+--------------------------------+
Post
+----+--------+---------------------------------+
| id |title | Body |
+----+--------+---------------------------------+
| 1 | test | Bla bla bla |
| 2 | Hello | Bla bla bla |
| 3 | Its me | Bla bla bla |
+----+--------+---------------------------------+
Post_Annex
+----+--------+----------+---------------------+
| id | IdPost | IdField | Value |
+----+--------+----------+---------------------+
| 1 | 1 | 1 | 50 |
| 2 | 1 | 2 | 21 |
| 3 | 1 | 3 | 3 |
| 4 | 2 | 1 | 10 |
| 5 | 2 | 2 | 2 |
| 6 | 2 | 3 | 150 |
| 7 | 3 | 1 | 1 |
| 8 | 3 | 2 | 25 |
| 9 | 3 | 3 | 15 |
+----+--------+--------------------------------+
我希望从Post和Post_Annex表中选择数据,按 Post 表的 Id 对它们进行分组并按价格排序(这意味着当 Post_Annex 表的 IdField 等于 1 时)。所以这是我的查询:
SELECT p.title
FROM Post p
LEFT JOIN Post_Annex pa
ON p.id = pa.IdPost
GROUP BY p.id
ORDER BY case when pa.IdField = 1 then 0 else 1, pa.Value DESC
这给了我这个顺序的结果:its Me | Hello | test
这可以。但即使我尝试按宽度(IdField = 2)或高度(IdField = 3)排序,它也给了我相同的结果。
请掌握任何想法来帮助我解决这个问题?提前致谢。