Mysql SQL查询使用order子句从两个单独的表中进行选择
我有两张桌子:Mysql SQL查询使用order子句从两个单独的表中进行选择,mysql,sql,Mysql,Sql,我有两张桌子: tickets ticket_updates 我正在尝试运行一个查询,其中显示来自tickets表的数据和ticket\u updates表中最新的datetime SELECT * from tickets where status = 'Completed' order by ??? DESC LIMIT 0,50 tickets表中的ticketnumber与ticket\u更新表中的ticketnumber匹配一种方法是汇总ticket\u更新表以获得所需内容: S
tickets
ticket_updates
我正在尝试运行一个查询,其中显示来自tickets表的数据和ticket\u updates
表中最新的datetime
SELECT * from tickets where status = 'Completed' order by ??? DESC LIMIT 0,50
tickets
表中的ticketnumber
与ticket\u更新
表中的ticketnumber
匹配一种方法是汇总ticket\u更新
表以获得所需内容:
SELECT * from tickets where status = 'Completed' order by ??? DESC LIMIT 0,50
select t.*
from tickets t join
(select ticketid, max(datetime) as max_datetime
from ticket_updates
group by ticketid
) tu
on t.ticketid = tu.max_datetime
order by tu.max_datetime desc
limit 0, 50;
试试这个。连接两个表,然后按以下顺序排序:
SELECT a.*
from tickets a, ticket_updates b
where a.ticketnumber = b.ticketnumber and
a.status = 'Completed'
order by b.YOUR_DATETIME_FIELD DESC LIMIT 0,50
也许对你有帮助。