如何从两个表中按日期时间字段顺序从两个表中选择字段?

发布于 2024-11-15 05:05:50 字数 324 浏览 1 评论 0原文

我的餐桌信息: 表“entry1”有字段“id”“title”“username”“date_post”和其他字段 表“entry2”具有字段“id”“title”“username”“date_post”以及其他

包含更多信息的字段: 1) 表“entry1”中的“id”与表“entry2”中的“id”不同 2)两个表中的字段“date_post”都是我想要的日期时间类型

:我想从两个表中选择“id”“title”“username”和“date_post” 并按语句中两个表中的“date_post”DESC 进行排序。

抱歉,我对 PHP 很陌生,而且我的英语不好。很难问。

my table info:
table 'entry1' have fields 'id' 'title' 'username' 'date_post' and other fields
table 'entry2' have fields 'id' 'title' 'username' 'date_post' ant other fields

with more info:
1) 'id' in table 'entry1' is not the same as 'id' in table 'entry2'
2) field 'date_post' from both table are type of datetime

what i want: I want to select 'id' 'title' 'username' and 'date_post' from both table
and order by 'date_post' DESC from both table in a statement.

Sorry all, I'm very new to PHP and my english is not good. It's very difficult to ask.

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

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

发布评论

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

评论(2

偏闹i 2024-11-22 05:05:50

我的工作代码是:

$sql="(SELECT title, date_post FROM entry1 WHERE user_id=$user_id) UNION 
      (SELECT title, date_post FROM entry2 WHERE user_id=$user_id) UNION
      (SELECT title, date_post FROM entry3  WHERE user_id=$user_id)
      ORDER  BY date_post DESC";
$result=mysql_query($sql);

My working code is:

$sql="(SELECT title, date_post FROM entry1 WHERE user_id=$user_id) UNION 
      (SELECT title, date_post FROM entry2 WHERE user_id=$user_id) UNION
      (SELECT title, date_post FROM entry3  WHERE user_id=$user_id)
      ORDER  BY date_post DESC";
$result=mysql_query($sql);
小兔几 2024-11-22 05:05:50

假设两个表具有相同的列名和类型。试试这个。

SELECT * FROM entry1
UNION
SELECT * FROM entry2
ORDER BY date_post DESC

Assume that both table have same identical column names and types. Try this.

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