[英]SQL Inner join with limit on subquery not to affect query
I'm running a query in MySQL with an INNER JOIN
that has a LIMIT
on the subquery我正在 MySQL 中运行一个查询,其中一个INNER JOIN
在子查询上有一个LIMIT
The problem is, that the LIMIT on the subquery is affecting the number of rows returned.问题是,子查询上的 LIMIT 会影响返回的行数。
I want to select all rows from table 1 ( tickets
) where the last row in ticket_updates
relevant ( t.ticketnumber = tu.ticketnumber
) was not numeric in column contact_name
我想从表 1( tickets
)中选择所有行,其中ticket_updates
中的最后一行相关( t.ticketnumber = tu.ticketnumber
)在列contact_name
不是数字
SELECT t.*
FROM tickets t
JOIN
( SELECT ticketnumber
FROM ticket_updates
WHERE type = 'update'
AND concat('', contact_name * 1) <> contact_name
ORDER
BY sequence DESC
LIMIT 1
) tu
ON t.ticketnumber = tu.ticketnumber
WHERE t.status <> 'Completed'
AND LOWER(t.department) = 'support';
But the results shown just return the 1 row但显示的结果只返回 1 行
There are multiple rows in ticket_updates
that relate to each row in tickets
based on tickets.ticketnumber =
ticket_updates.ticketnumber` ticket_updates
中有多行与基于 ticket.ticketnumber tickets.ticketnumber =
ticket_updates.ticketnumber` 的tickets
的每一行相关
the contact_name
column can either be a string or integer. contact_name
列可以是字符串或整数。 I picked up the concat('', contact_name * 1) <> contact_name
from another SO Post which tells me whether the value is numeric or not.我从另一个 SO Post 中获取了concat('', contact_name * 1) <> contact_name
,它告诉我该值是否为数字。
So I want to pick up the latest row ( ORDER BY sequence DESC
) in ticket_updates
for each row in tickets
and see whether contact_name
is not numeric所以我想搭载了最新的行( ORDER BY sequence DESC
中) ticket_updates
在每一行tickets
,看是否contact_name
是不是数字
Your query selects one row because (as you concluded yourself) your subquery is limited to one result.您的查询选择一行是因为(正如您自己总结的那样)您的子查询仅限于一个结果。
what you want is probably something similar to what can be found in this answer (I assumed that you want the entry with biggest value for sequence
, if the contratry, change to MIN
)您想要的可能与此答案中的内容类似(我假设您想要具有最大值的条目sequence
,如果相反,请更改为MIN
)
with a subquery (there should be an adaptation from the shortest more optimized query in the cited answer, but let's see if this one works first):使用子查询(应该对引用的答案中最短的更优化查询进行改编,但让我们先看看这个查询是否有效):
SELECT t.*
FROM tickets t
INNER JOIN ( SELECT ticketnumber, MAX(tu.sequence) AS maxSequence
FROM ticket_updates tu
WHERE tu.type = 'update' AND concat('', tu.contact_name * 1) <> tu.contact_name
GROUP BY ticketnumber ) tu2
ON (t.ticketnumber = tu2.ticketnumber)
WHERE t.status <> 'Completed'
AND LOWER(t.department) = 'support';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.