使用连接查询中的字段值作为列

发布于 2024-11-14 19:23:37 字数 823 浏览 0 评论 0原文

我有两张描述可以扩展到子类的数据的 MySQL 表,一张描述父类数据,另一张将元数据字段描述为一对多关系。

page中的示例行:

id   | name
-----+-----------------------
123  | Example page
999  | Another page

page_metadata中的示例行:

page_id | key        | value
--------+------------+----------------
123     | picture    | test.jpg
123     | video      | example.avi
123     | sound      | sound.mp3
999     | picture    | something.jpg

问题是否有某种方法可以以这种格式获取所有这些数据?

id   | name             | picture       | video       | sound
-----+------------------+---------------+-------------+------------------
123  | Example page     | test.jpg      | example.avi | sound.mp3
999  | Another page     | something.jpg | NULL        | NULL

I have two MySQL tables describing data that can be extended into subclasses, one that describes the parent class data and another one describing metadata fields as one-to-many relationships.

Example rows from table page:

id   | name
-----+-----------------------
123  | Example page
999  | Another page

Example rows from table page_metadata:

page_id | key        | value
--------+------------+----------------
123     | picture    | test.jpg
123     | video      | example.avi
123     | sound      | sound.mp3
999     | picture    | something.jpg

Question: Is there some way to fetch all this data in this format?

id   | name             | picture       | video       | sound
-----+------------------+---------------+-------------+------------------
123  | Example page     | test.jpg      | example.avi | sound.mp3
999  | Another page     | something.jpg | NULL        | NULL

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罪#恶を代价 2024-11-21 19:23:37
SELECT pm.page_id as id
  , p.name
  , max(IF(pm.key = 'picture', pm.value, null)) as picture
  , max(IF(pm.key = 'video', pm.value, null)) as video
  , max(IF(pm.key = 'sound', pm.value, null)) as sound
FROM page p
INNER JOIN page_metadata pm ON (p.id = pm.page_id)
GROUP BY p.id
SELECT pm.page_id as id
  , p.name
  , max(IF(pm.key = 'picture', pm.value, null)) as picture
  , max(IF(pm.key = 'video', pm.value, null)) as video
  , max(IF(pm.key = 'sound', pm.value, null)) as sound
FROM page p
INNER JOIN page_metadata pm ON (p.id = pm.page_id)
GROUP BY p.id
情丝乱 2024-11-21 19:23:37

尝试一下

SELECT page.id, page.name, picture.value, video.value, sound.value
FROM page
LEFT OUTER JOIN (SELECT page_id, value FROM page_metadata WHERE key = 'picture') as picture
  ON page.id = picture.page_id
LEFT OUTER JOIN (SELECT page_id, value FROM page_metadata WHERE key = 'video') as video
  ON page.id = video.page_id
LEFT OUTER JOIN (SELECT page_id, value FROM page_metadata WHERE key = 'sound') as sound
  ON page.id = sound.page_id

应该会得到你需要的东西

Try

SELECT page.id, page.name, picture.value, video.value, sound.value
FROM page
LEFT OUTER JOIN (SELECT page_id, value FROM page_metadata WHERE key = 'picture') as picture
  ON page.id = picture.page_id
LEFT OUTER JOIN (SELECT page_id, value FROM page_metadata WHERE key = 'video') as video
  ON page.id = video.page_id
LEFT OUTER JOIN (SELECT page_id, value FROM page_metadata WHERE key = 'sound') as sound
  ON page.id = sound.page_id

should get what you need

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文