我的查询是这样的:
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;
结果如下:
+--------------+-----------+
| order_number | dr_amount |
+--------------+-----------+
| 17047 | 1307 |
+--------------+-----------+
这是我想进一步处理的结果。 现在我想为此添加一个子查询。我需要在DATA WHERE oi_amount&gt; 0中检查order_number(17047)。 所以我尝试对此进行查询:
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;
但这给了我:
+--------------+-----------+
| order_number | dr_amount |
+--------------+-----------+
| 17046 | 653 |
+--------------+-----------+
这不是我想要的。我想从该查询中返回无结果,因为order_number 17047具有oi_amount&lt; = 0(或NULL) 我知道当order_number 17047不匹配时,它会跳到匹配的下一行,这就是order_number为17046的行。 所以我想在LIMIT 1中添加一个子查询。像这样:
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);
但是这会引发错误:
This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
我的表格如下:
+--------+--------------+-------------+------------+------------+-----------+-----------+
| 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 |
+--------+--------------+-------------+------------+------------+-----------+-----------+
正如你所看到的,order_number 17047没有oi_amount记录,这就是为什么我不想返回结果/空结果。
我对如何使用JOIN知之甚少,但也许这就是要走的路?我已经尝试过som查询,但我不确定这是否会起作用(我所有的JOIN查询都有一些语法错误,所以我放弃了)
祝你好运 尼克拉斯
答案 0 :(得分:1)
尝试使用join
代替:
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;
请注意,我更改了您的运算符以符合SQL标准:<>
而不是!=
和and
而不是&&
。此外,如果看起来像数字的常数确实是数字,那么您就不需要单引号。