![](/img/trans.png)
[英]select the nearest date in mysql using codeigniter from two different tables?
[英]Sorting content from different MySQL tables by date
我有3个不同的表:
新闻
歌曲
访问
我正在尝试在我的网站上创建一个时间轴,以显示由我的3个表格中的pubdate排序的所有内容。 有没有办法在MySQL中做到这一点?
也许创建一个“通用”表,其中包括从我的3个其他表中按日期排序的所有ID? 然后我应该用PHP链接所有元素。 功耗不是太大了吗?
试试这个,看看它是否符合你的需求:
SELECT
`news_id` AS `id`,
`news_content` AS `content`,
`news_pubdate` AS `pubdate`,
"" AS `person`,
"news" AS `type`
FROM `news`
UNION
SELECT
`song_id` AS `id`,
`song_link` AS `content`,
`song_pubdate` AS `pubdate`,
"" AS `person`,
"song" AS `type`
FROM `song`
UNION
SELECT
`interview_id` AS `id`,
`interview_content` AS `content`,
`interview_pubdate` AS `pubdate`,
`interview_artist` AS `person`,
"interview" AS `type`
FROM `interview`
ORDER BY `pubdate`;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.