mySQL查询——按日期排序
我正在合并两个表,每个表都有不同的事件数据。每张桌子都有一个活动日期。
如何组合这些事件并按日期对它们进行无缝排序?
SELECT
t1.eventName,
t1.eventTime,
t2.evntName,
t2.evntTime
FROM t1
LEFT JOIN t2 ON (t1.eventID = t2.eventID)
ORDER BY ??? DESC
LIMIT 10
I am merging two tables, each one with different events data. Each table has a date for the events.
How do I combine these events to sort them seamlessly by date?
SELECT
t1.eventName,
t1.eventTime,
t2.evntName,
t2.evntTime
FROM t1
LEFT JOIN t2 ON (t1.eventID = t2.eventID)
ORDER BY ??? DESC
LIMIT 10
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@Gonzalo 很接近,但还不够。您需要将每个表放入子查询中,并为最终结果中所需的每个列指定别名。如果一个表没有您需要从另一个表中获得的列,请使用文字并为其指定别名。
将任何
WHERE
子句放入子查询内部。@Gonzalo is close but not quite there. You need to put each table into subqueries and alias each column you want in the final result. If one table doesn't have a column that you need from another use a literal and alias it.
Put any
WHERE
clauses inside the subqueries.不是 100% 确定您要在这里做什么,但也许是这样:
ETA:
看起来您无法直接将日期与 GREATEST 进行比较,因此您可能需要这样做:
Not 100% sure what you are trying to do here, but maybe this:
ETA:
Looks like you can't compare dates directly with GREATEST, so you might need to do this:
您正在连接这些表,这将导致两个表的记录合并在一行中。如果你真的想合并这样的表,你必须做的是 UNION 查询。像这样的事情:
因此,您将两个表的结果合并在子查询中,然后对其进行排序。
祝你好运!
You're joining the tables, wich will result in records of both tables combined in one row. What you have to do if you really want to merge tables like that, is an UNION query. Something like this:
So, you merge the results of both tables in a subquery, and the you sort it.
Good luck!