LIMIT & 问题IN/ALL/ANY/SOME 子查询

发布于 2024-10-17 09:03:16 字数 452 浏览 2 评论 0原文

我有这个查询:

SELECT count(cp.CxID) as intSmokers 
FROM CustPrimarySmoking cp 
JOIN Customer c ON cp.CxID = c.CustomerID 
WHERE 
cp.CxID IN (SELECT CxID FROM CustPrimarySmoking WHERE CxID = cp.CxID LIMIT 1, 9999)

想法是计数将基于嵌套查询的结果,该查询检索该客户的所有记录(除了第一条记录)。

然而,我收到这个错误,我认为这是非常严重的:

1235 - 此版本的 MySQL 尚不支持“LIMIT &” IN/ALL/ANY/SOME 子查询'

有谁知道还有其他方法可以做到这一点吗?

谢谢

I have this query:

SELECT count(cp.CxID) as intSmokers 
FROM CustPrimarySmoking cp 
JOIN Customer c ON cp.CxID = c.CustomerID 
WHERE 
cp.CxID IN (SELECT CxID FROM CustPrimarySmoking WHERE CxID = cp.CxID LIMIT 1, 9999)

The idea being that the count will be based on the results of the nested query which retrieves all the records for that customer EXCEPT the first record.

HOWEVER, I get this error, which I think is pretty terminal:

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

Does anyone know of any other way of doing this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

大姐,你呐 2024-10-24 09:03:16

这就是您需要继续的方式。请参阅我已经完成的示例。

mysql> select * from test;
+------+-------+
| id   | name  |
+------+-------+
|    1 | name1 |
|    2 | name2 |
|    3 | name3 |
|    4 | name4 |
+------+-------+
4 rows in set (0.00 sec)

mysql> select * from test1;
+------+------+--------+
| id   | tid  | name2  |
+------+------+--------+
|    1 |    2 | name11 |
|    2 |    3 | name12 |
|    3 |    4 | name13 |
+------+------+--------+
3 rows in set (0.00 sec)

mysql> select
    ->  t1.name
    -> from
    ->  test t1
    -> join
    ->  test1 t2 on t2.tid = t1.id
    -> join
    ->  (select id from test where id <4 limit 3) as tempt on tempt.id = t1.id;
+-------+
| name  |
+-------+
| name2 |
| name3 |
+-------+
2 rows in set (0.00 sec)

希望这有帮助。

This is how you need to proceed. See the example that I've worked out.

mysql> select * from test;
+------+-------+
| id   | name  |
+------+-------+
|    1 | name1 |
|    2 | name2 |
|    3 | name3 |
|    4 | name4 |
+------+-------+
4 rows in set (0.00 sec)

mysql> select * from test1;
+------+------+--------+
| id   | tid  | name2  |
+------+------+--------+
|    1 |    2 | name11 |
|    2 |    3 | name12 |
|    3 |    4 | name13 |
+------+------+--------+
3 rows in set (0.00 sec)

mysql> select
    ->  t1.name
    -> from
    ->  test t1
    -> join
    ->  test1 t2 on t2.tid = t1.id
    -> join
    ->  (select id from test where id <4 limit 3) as tempt on tempt.id = t1.id;
+-------+
| name  |
+-------+
| name2 |
| name3 |
+-------+
2 rows in set (0.00 sec)

Hope this helps.

摇划花蜜的午后 2024-10-24 09:03:16

您不需要使用子查询来检索所有记录,只需排除第一个记录:

SELECT count(cp.CxID) as intSmokers
来自 CustPrimarySmoking cp
JOIN 客户 c ON cp.CxID = c.CustomerID
其中 cp.CxID > (SELECT cxID FROM CustPrimarySmoking ORDER BY cxID LIMIT 1)

假设 cxid 是数字

You don't need to use the subquery to retrieve all the records, just exclude the first one:

SELECT count(cp.CxID) as intSmokers
FROM CustPrimarySmoking cp
JOIN Customer c ON cp.CxID = c.CustomerID
WHERE cp.CxID > (SELECT cxID FROM CustPrimarySmoking ORDER BY cxID LIMIT 1)

Assuming that cxid is numeric

青巷忧颜 2024-10-24 09:03:16

您还可以双重嵌套内部查询来绕过此限制,请参阅:

Mysql delete statements with limit

You can also double-nest the inner query to get around this restriction, see:

Mysql delete statement with limit

↘紸啶 2024-10-24 09:03:16

如果您想获得“每组的前 N ​​行”之类的信息,那么这种限制会很痛苦。但就你而言,即使可能,我也不会使用该功能。您尝试做的是计算除每个CxID 一行之外的所有行。您只需减去不同 CustomerID 的数量,即 count(DISTINCT cp.CxID)。所以你的最终查询应该像这样简单:

SELECT count(cp.CxID) - count(DISTINCT cp.CxID) as intSmokers 
FROM CustPrimarySmoking cp

This limitation is a pain if you want to get something like "top N rows for each group". But in your case I wouldn't use that feature even if it were possible. What you try to do is to count all rows except of one row each CxID. All you need is just to subtract the number of distinct CustomerIDs, which is count(DISTINCT cp.CxID). So your final query should be as simple as:

SELECT count(cp.CxID) - count(DISTINCT cp.CxID) as intSmokers 
FROM CustPrimarySmoking cp
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文