I'm running a query in MySQL with an INNER JOIN
that has a LIMIT
on the subquery
The problem is, that the LIMIT on the subquery is affecting the number of rows returned.
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
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
There are multiple rows in ticket_updates
that relate to each row in tickets
based on tickets.ticketnumber =
ticket_updates.ticketnumber`
the contact_name
column can either be a string or integer. 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 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
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
)
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';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.