简体   繁体   中英

mysql - want to use LIMIT in IN subquery, how to workaround?

I have a query that is like this:

SELECT order_number,dr_amount FROM data WHERE order_number IN (select order_number from data where dr_machine='KBA R-' && dr_code='Tryckning' && dr_amount IS NOT NULL && dr_amount!=0 group by order_number having count(*)<2) && dr_machine='KBA R-' && dr_code='Tryckning' && dr_date>='1381442400' && id>'397433' && order_number!='16952' ORDER BY id limit 1;

The result is like this:

+--------------+-----------+
| order_number | dr_amount |
+--------------+-----------+
|        17047 | 1307      |
+--------------+-----------+

That is the result I would like to handle further. Now i would like to add a subquery to this. I need to check that order_number (17047) in DATA WHERE oi_amount>0. So I try to make the query to this:

SELECT order_number,dr_amount FROM data WHERE order_number IN (select order_number from data where dr_machine='KBA R-' && dr_code='Tryckning' && dr_amount IS NOT NULL && dr_amount!=0 group by order_number having count(*)<2) && dr_machine='KBA R-' && dr_code='Tryckning' && dr_date>='1381442400' && id>'397433' && order_number!='16952' && order_number in (select order_number from data where oi_amount>0) ORDER BY id limit 1;

But that gives me:

+--------------+-----------+
| order_number | dr_amount |
+--------------+-----------+
|        17046 | 653       |
+--------------+-----------+

And that is not what I want. I want to return no results from that query, because order_number 17047 has oi_amount<=0 (or NULL) I understand that when order_number 17047 doesn't match, then it skips to next row that matches, and that is the row with order_number 17046. So I would like to add a subquery with LIMIT 1. Like this:

SELECT order_number,dr_amount FROM data WHERE order_number IN (select order_number from data where dr_machine='KBA R-' && dr_code='Tryckning' && dr_amount IS NOT NULL && dr_amount!=0 group by order_number having count(*)<2) && order_number in (select order_number from data where dr_machine='KBA R-' && dr_code='Tryckning' && dr_date>='1381442400' && id>'397433' && order_number!='16952' ORDER BY id limit 1);

But that throws the error:

This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

My table looks like this:

+--------+--------------+-------------+------------+------------+-----------+-----------+
| id     | order_number | dr_worker   | dr_code    | dr_machine | dr_amount | oi_amount |
+--------+--------------+-------------+------------+------------+-----------+-----------+
| 332144 |        16952 | NULL        | NULL       | NULL       | NULL      | 2000      |
| 397432 |        16952 | Gustafsson, | Intag      | KBA R-     | 1         | NULL      |
| 397433 |        16952 | Gustafsson, | Tryckning  | KBA R-     | 1307      | NULL      |
| 397434 |        17047 | Gustafsson, | Intag      | KBA R-     | 1         | NULL      |
| 397435 |        17047 | Gustafsson, | Tryckning  | KBA R-     | 1307      | NULL      |
+--------+--------------+-------------+------------+------------+-----------+-----------+

As you can see order_number 17047 has no record of oi_amount, thats why I would like to return no result/empty result.

I have little knowledge of how to use JOIN, but maybe thats the way to go? I've tried som queries but I'm not sure if that even will work (all my queries with JOIN have had some syntax error, so I gave up)

Best regards Niclas

Try using a join instead:

SELECT order_number, dr_amount
FROM data d join
     (select order_number
      from data
      where dr_machine = 'KBA R-' and
            dr_code = 'Tryckning' and
            dr_amount IS NOT NULL and
            dr_amount <> 0
      group by order_number
      having count(*) < 2
     ) i
     on d.order_number = i.order_number
WHERE d.dr_machine = 'KBA R-' and
      d.dr_code = 'Tryckning' and
      d.dr_date >= '1381442400' and
      d.id > '397433' and
      d.order_number <> '16952'
ORDER BY id
limit 1;

Note that I changed your operators to conform to SQL standards: <> instead of != and and instead of && . In addition, if the constants that look like numbers really are numbers, you don't need single quotes.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM