首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NOT功能不适用于我的mysql版本。

NOT功能不适用于我的mysql版本。
EN

Stack Overflow用户
提问于 2016-02-23 02:40:44
回答 1查看 11关注 0票数 1

我有一个在mysql版本5.5.44-0上工作的语句,但是它在mysql 4.0.24中根本不起作用。

有什么想法我可以改变使它向后兼容吗?

代码语言:javascript
复制
select distinct cust_id
from billing where billing.cust_id
not in (
select distinct billing_id from internet
);

这些是桌子

代码语言:javascript
复制
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版本

代码语言:javascript
复制
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版本

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-23 02:58:15

哇,不幸的是,您必须使用MySQL 4.0,这是绝对古老的,不支持大多数类型的子查询或子选择。您可以将查询重写为LEFT JOIN,其中WHERE子句对NULL值进行筛选,以指示与NOT IN ()不匹配的值。

代码语言:javascript
复制
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

如果您可以在该服务器上获得更新的MySQL版本,则强烈建议您使用该版本。4.0发行版可以追溯到2002年,也就是MySQL生命周期的早期,它将失去许多更重要的特性和安全性改进。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35567797

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档