我有 2 个表 - 帖子和元
帖子结构:
+---------+---------+
| id | title |
+---------+---------+
元结构:
+---------+---------+---------+
| post | key | value |
+---------+---------+---------+
我需要选择 meta.key 为“end”或“result”的记录,并按 post.id 组合(如果同时存在“end”和“result”元键)
这是我的尝试:
SELECT
posts.id,
posts.title,
meta.post,
meta.`value`,
CASE meta.`key` WHEN 'result' THEN value END AS 'result',
CASE meta.`key` WHEN 'end' THEN value END AS 'end'
FROM
posts
INNER JOIN meta ON posts.id = meta.post
WHERE meta.`key` IN ('result', 'end')
它返回两条单独的记录,一条带有 meta.key “end”,一条带有“result”。
+------------+------------+------------+------------+------------+------------+
| id | title | post | value | result | end |
+------------+------------+------------+------------+------------+------------+
| 1 | Untitled | 1 | 5 | (Null) | 1344240000 |
+------------+------------+------------+------------+------------+------------+
| 1 | Untitled | 1 | 88:79 | 88:79 | (Null) |
+------------+------------+------------+------------+------------+------------+
我需要的是将这两条记录合并为一条:
+------------+------------+------------+------------+------------+------------+
| id | title | post | value | result | end |
+------------+------------+------------+------------+------------+------------+
| 1 | Untitled | 1 | - | 88:79 | 1344240000 |
+------------+------------+------------+------------+------------+------------+