函数中的NOT不适用于我的mysql版本
我有一个在mysql版本函数中的NOT不适用于我的mysql版本,mysql,Mysql,我有一个在mysql版本5.5.44-0上运行的语句,但是在mysql4.0.24 有什么想法我可以改变,使它向后兼容 select distinct cust_id from billing where billing.cust_id not in ( select distinct billing_id from internet ); 这些是桌子 mysql> select * from billing; +----+---------+-------+ | id | cust_i
5.5.44-0
上运行的语句,但是在mysql4.0.24
有什么想法我可以改变,使它向后兼容
select distinct cust_id
from billing where billing.cust_id
not in (
select distinct billing_id from internet
);
这些是桌子
mysql> select * from billing;
+----+---------+-------+
| id | cust_id | cost |
+----+---------+-------+
| 2 | 34 | 500 |
| 3 | 12 | 67700 |
| 4 | 99 | 100 |
| 5 | 99 | 1700 |
| 6 | 1450 | 800 |
| 7 | 88 | 0 |
| 8 | 222 | 5100 |
| 9 | 288 | 5100 |
| 10 | 329 | 5100 |
+----+---------+-------+
mysql> select * from internet;
+----+------------+------+
| id | billing_id | cost |
+----+------------+------+
| 1 | 1450 | 900 |
| 2 | 99 | 900 |
| 3 | 899 | 900 |
| 4 | 329 | 900 |
+----+------------+------+
在版本5.5.44-0中
:
mysql> select distinct cust_id
-> from billing where billing.cust_id
-> not in (
-> select distinct billing_id from internet
-> );
+---------+
| cust_id |
+---------+
| 34 |
| 12 |
| 88 |
| 222 |
| 288 |
+---------+
在版本4.0.24
中:
mysql> select distinct cust_id
-> from billing where billing.cust_id
-> not in (
-> select distinct billing_id from internet
-> );
ERROR 1064 (HY000): You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'select distinct billing_id from internet
)' at line 4
哇,不幸的是,您必须使用MySQL 4.0,它绝对是古老的,不支持大多数类型的子查询或子选择。您可以将查询重写为
左连接
,其中WHERE
子句过滤NULL
值,以指示与notin()等价的不匹配项
如果您能够在该服务器上获得更新的MySQL版本,则强烈建议您使用它。4.0版本线可以追溯到2002年,也就是MySQL生命周期的早期,它将缺少很多更重要的特性和安全改进
SELECT
distinct cust_id
FROM
billing
-- Express the equivalence you matched with NOT IN()
-- as a left join ON condition
LEFT JOIN internet ON billing.cust_id = internet.billing_id
WHERE
-- A NULL value on the joined table indicates
-- the relationship doesn't exist / there is no matching
-- `billing_id` record in `internet` for the `cust_id`
internet.billing_id IS NULL