简体   繁体   English

从两个表中选择,从第二个表中按一行排序

[英]select from two tables ordering by one row from second table

I have two tables, tickets and ticket_updates 我有两个表, ticketsticket_updates

There are multiple rows in ticket_updates for each one row in tickets 有在多行ticket_updates在每一个行tickets

how can i select from tickets ordering by datetime in ticket_updates 如何在ticket_updatesdatetime时间从订购ticketsticket_updates

i want to order by datetime in ticket_updates from the earliest datetime (DESC) 我想从最早的datetime (DESC)在ticket_updatesdatetime时间进行订购

i thought the following may work, however it takes too long to run in PHPMyAdmin and i get an internal server error: 我以为以下方法可能有效,但是在PHPMyAdmin中运行需要太长时间,并且出现内部服务器错误:

SELECT t.ticketnumber, t.subject, t.contact_name FROM tickets t JOIN ticket_updates tu on t.ticketnumber = tu.ticketnumber group by tu.ticketnumber order by tu.datetime DESC

You could do something like this: 您可以执行以下操作:

select t.*
from tickets t
order by (select min(datetime) from ticket_updates tu where tu.ticketid = t.ticketid);

EDIT: 编辑:

You can also try with a group by : 您也可以尝试以下group by

select t.*
from tickets t join
     (select ticketid, min(datetime) as mindt
      from ticket_updates tu
      group by ticketid
     ) tu
     on tu.ticketid = t.ticketid
group by tu.mindt desc;

Or, the original query can take advantage of an index on ticket_updates(ticketid, datetime) . 或者,原始查询可以利用ticket_updates(ticketid, datetime)上的索引。

I'd think you'd want something like this, which is pretty much what you'd posted after the edit. 我认为您想要这样的东西,这几乎是您编辑后发布的内容。 If it is running too slow you might need to run an EXPLAIN for the query and add an appropriate index. 如果运行太慢,则可能需要对查询运行EXPLAIN并添加适当的索引。 Either to datetime or potentially a compound key to ticketnumber/datetime as Gordon suggested. 就像戈登建议的那样,要么是日期时间,要么是票号/日期时间的复合键。

SELECT t.ticketnumber, t.subject, t.contact_name FROM ticket_updates tu
INNER JOIN tickets t ON t.ticketnumber = tu.ticketnumber
GROUP BY tu.ticketnumber
ORDER BY tu.datetime DESC

or 要么

SELECT t.ticketnumber, t.subject, t.contact_name FROM ticket_updates tu
INNER JOIN tickets t using(ticketnumber)
GROUP BY tu.ticketnumber
ORDER BY tu.datetime DESC

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从两个表中选择数据,而两个表只为第一表的一行选择了第二表的第一行 - How to SELECT data from two tables which only first row of second table is selected for one row of first table 从两个表中进行选择,第二个表的限制为LIMIT 1 - SELECT from two tables with LIMIT 1 for second table 如何从两个表中进行选择,并且仅显示一个表中的一项,而第二个表中的多项呢? - How to select from two tables and display only one item from 1 table and many from the second table? 如果第二个表中至少有4条记录,则从两个表中选择记录 - Select records from two tables if atleast 4 records in second table SQL 从两个表中选择数据,一行 -> 多行 - SQL Select data from two tables, one row -> multiple rows Laravel连接2个表,第一个表一个数据,第二个表多个数据 - Laravel Join 2 tables , one data from first table and multiple row from second table 从两个表中选择,第二个表中的行应在第一个表中显示为列 - Select from two tables where rows from second table should be shown as columns in first 如何从两个表中获取数据,第二个表显示行数 - How can I get data from two tables and the second table shows row count MYSQL - 两个表从每个用户的第二个表最后跟踪的行中获取记录 - MYSQL - Two tables get record from second table last tracked row for each user 从两个表中仅选择满足第二个表中多个条件的行 - Select from two tables only that rows that satisfies multiple conditions in second table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM