MySQL 关联查询的 Having 子句不起作用
MySQL Having clause with a Correlated query is not working
我的 having 子句有问题。我想比较具有相同出价但不同 sens 字段的两个总和
我得到这个输出:
SELECT v.bid
FROM v1 v
WHERE sens = 'c'
GROUP BY bid
HAVING Sum(mont) < (SELECT Sum(l.mont)
FROM v1 l
WHERE sens = 'd'
AND l.bid = v.bid group by l.bid);
ERROR 1054 (42S22): Field 'v.bid unknown in field list`
编辑:V1 是一个视图,我使用别名 l 和 v 试图将子查询连接到主查询
抱歉,伙计们,谢谢大家的回答,我在原始 table 的列中遇到了问题,现在已经解决了:)
不需要从这个 table 中进行两次选择,您可以使用 CASE EXPRESSION
来达到这个目的:
SELECT v.bid FROM v1 v
GROUP BY v.bid
HAVING SUM(CASE WHEN v.sens = 'c' THEN v.mont ELSE 0 END) <
SUM(CASE WHEN v.sens = 'd' THEN v.mont ELSE 0 END)
AND COUNT(CASE WHEN v.sens = 'c' THEN 1 END) > 0
subselect 中的别名不是 visible/available 所以你会得到列未找到错误,但你可以用这种方式重构你的 queru
select v.bid, t.tot
from v1 v
Join (
select bid, sum(mont) tot
from v1
where sens = 'd'
group by bid
) t on t.bid = v.bid
where v.sens = 'c'
and v.bid < tot
我在列的可见性方面遇到问题,因为视图声明不正确,我向大家道歉:'(我感觉很糟糕:(
脚本:
mysql> select * from op;
+------+------+------+------+
| bid | cid | sens | mont |
+------+------+------+------+
| 1 | 1 | c | 2000 |
| 1 | 1 | c | 2000 |
| 1 | 1 | c | 2000 |
| 1 | 1 | c | 2000 |
| 1 | 2 | c | 2000 |
| 1 | 3 | c | 2000 |
| 2 | 3 | c | 2000 |
| 1 | 1 | d | 2000 |
| 2 | 3 | d | 4000 |
+------+------+------+------+
9 rows in set (0.00 sec)
create view v1 as ( select bid , cid , sens , sum(mont) as sumcli from op group by bid,cid,sens);
mysql> select * from v1 ;
+------+------+------+--------+
| bid | cid | sens | sumcli |
+------+------+------+--------+
| 1 | 1 | c | 8000 |
| 1 | 1 | d | 2000 |
| 1 | 2 | c | 2000 |
| 1 | 3 | c | 2000 |
| 2 | 3 | c | 2000 |
| 2 | 3 | d | 4000 |
+------+------+------+--------+
6 rows in set (0.00 sec)
mysql> SELECT v.bid
-> FROM v1 v
-> WHERE sens = 'c'
-> group by v.bid
-> HAVING Sum(v.sumcli) < (SELECT Sum(l.sumcli)
-> FROM v1 l
-> WHERE sens = 'd'
-> AND l.bid = v.bid group by l.bid);
+------+
| bid |
+------+
| 2 |
+------+
1 row in set (0.00 sec)
我的 having 子句有问题。我想比较具有相同出价但不同 sens 字段的两个总和 我得到这个输出:
SELECT v.bid
FROM v1 v
WHERE sens = 'c'
GROUP BY bid
HAVING Sum(mont) < (SELECT Sum(l.mont)
FROM v1 l
WHERE sens = 'd'
AND l.bid = v.bid group by l.bid);
ERROR 1054 (42S22): Field 'v.bid unknown in field list`
编辑:V1 是一个视图,我使用别名 l 和 v 试图将子查询连接到主查询
抱歉,伙计们,谢谢大家的回答,我在原始 table 的列中遇到了问题,现在已经解决了:)
不需要从这个 table 中进行两次选择,您可以使用 CASE EXPRESSION
来达到这个目的:
SELECT v.bid FROM v1 v
GROUP BY v.bid
HAVING SUM(CASE WHEN v.sens = 'c' THEN v.mont ELSE 0 END) <
SUM(CASE WHEN v.sens = 'd' THEN v.mont ELSE 0 END)
AND COUNT(CASE WHEN v.sens = 'c' THEN 1 END) > 0
subselect 中的别名不是 visible/available 所以你会得到列未找到错误,但你可以用这种方式重构你的 queru
select v.bid, t.tot
from v1 v
Join (
select bid, sum(mont) tot
from v1
where sens = 'd'
group by bid
) t on t.bid = v.bid
where v.sens = 'c'
and v.bid < tot
我在列的可见性方面遇到问题,因为视图声明不正确,我向大家道歉:'(我感觉很糟糕:(
脚本:
mysql> select * from op;
+------+------+------+------+
| bid | cid | sens | mont |
+------+------+------+------+
| 1 | 1 | c | 2000 |
| 1 | 1 | c | 2000 |
| 1 | 1 | c | 2000 |
| 1 | 1 | c | 2000 |
| 1 | 2 | c | 2000 |
| 1 | 3 | c | 2000 |
| 2 | 3 | c | 2000 |
| 1 | 1 | d | 2000 |
| 2 | 3 | d | 4000 |
+------+------+------+------+
9 rows in set (0.00 sec)
create view v1 as ( select bid , cid , sens , sum(mont) as sumcli from op group by bid,cid,sens);
mysql> select * from v1 ;
+------+------+------+--------+
| bid | cid | sens | sumcli |
+------+------+------+--------+
| 1 | 1 | c | 8000 |
| 1 | 1 | d | 2000 |
| 1 | 2 | c | 2000 |
| 1 | 3 | c | 2000 |
| 2 | 3 | c | 2000 |
| 2 | 3 | d | 4000 |
+------+------+------+--------+
6 rows in set (0.00 sec)
mysql> SELECT v.bid
-> FROM v1 v
-> WHERE sens = 'c'
-> group by v.bid
-> HAVING Sum(v.sumcli) < (SELECT Sum(l.sumcli)
-> FROM v1 l
-> WHERE sens = 'd'
-> AND l.bid = v.bid group by l.bid);
+------+
| bid |
+------+
| 2 |
+------+
1 row in set (0.00 sec)