【发布时间】:2013-03-30 13:57:17
【问题描述】:
我有两个用户帖子表,一个用于文本帖子,一个用于多媒体帖子,两个表的结构如下。
表格text_post
+--------+--------------+-----------+-----------+-----------------+-----------+
| postid | post_content | post_user | post_type | post_visibility | post_date |
+--------+--------------+-----------+-----------+-----------------+-----------+
表格multimedia_post
+-------+------------+--------+---------+---------+---------------+---------+
|post_id|post_content|post_url|post_user|post_type|post_visibility|post_date|
+-------+------------+--------+---------+---------+---------------+---------+
在 text_post post_content 中是用户发布的文本数据,在 media_post post_content 中是与多媒体文件相关联的标题文本,所以基本上post_content 是文本。所有的列都是通用的,只有在 media_post 中的 post_url 不同,我想将这两个表的结果组合如下
+-------+------------+--------+---------+---------+---------------+---------+
|post_id|post_content|post_url|post_user|post_type|post_visibility|post_date|
+-------+------------+--------+---------+---------+---------------+---------+
| 1 | some text | null | 1 | <type> | <visibility> | <date> |
+-------+------------+--------+---------+---------+---------------+---------+
| 1 | img_caption| <url> | 1 | <type> | <visibility> | <date> |
+-------+------------+--------+---------+---------+---------------+---------+
..and so on
此处返回的第一行来自text_post,因此post_url 设置为null,因为只有multimedia_post 具有该列...而第二行来自multimedia_post,因为有帖子的网址...
我怎样才能做到这一点?
【问题讨论】:
标签: mysql sql database-design join union-all