关于mysql:LIMIT问题 | 珊瑚贝

Problem with LIMIT & IN/ALL/ANY/SOME subquery


我有这个问题:

1
2
3
4
5
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 – This version of MySQL doesn’t yet support ‘LIMIT & IN/ALL/ANY/SOME subquery’

有没有人知道这样做的任何其他方式?

谢谢

  • 亲爱的… 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, 9999999
  • 重写您的查询 MySQL 不支持子查询中的 LIMIT。请参阅 dev.mysql.com/doc/refman/5.0/en/subquery-restrictions.html
  • AjReal,那行不通;您试图将整个查询的偏移量限制为 1,该查询仅检索一个结果(计数)。
  • Nishant,这就是我要问的!!!!有谁知道如何重写查询?!?
  • 在您的 CustPrimarySmoking 表中,显然每个客户(CxID)似乎有多个记录…表上是否还有另一列用于唯一性,例如自动增量列?


这就是您需要继续进行的方式。请参阅我制定的示例。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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)

希望这会有所帮助。

  • 我喜欢这个解决方案。通过查询从 240 秒提高到 0.03 秒。但是为什么那个子查询没有给出”不支持限制子查询”错误呢?
  • 太好了,这解决了我的问题


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

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)

假设 cxid 是数字

  • 那只是忽略了第一个;不是每个客户的第一个。
  • 然后将客户的加入也添加到子查询中:WHERE cp.CxID = (SELECT cps.cxID FROM CustPrimarySmoking AS cps JOIN Customer cust ON cps.CxID = cust.CustomerID ORDER BY cps.cxID LIMIT 1)
  • 这将只为每个客户选择第一条记录;我想忽略第一条记录并选择后续记录。
  • 这是最后一条评论中的错字。对于那个很抱歉。我的意思是只更改子查询,但将 > 替换为 =。将它 WHERE cp.CxID > 保留为原始查询中的状态,并且应该选择每个客户的所有记录,除了第一个


如果您想获得”每个组的前 N ??行”之类的东西,这个限制会很痛苦。但是在您的情况下,即使可能,我也不会使用该功能。您尝试做的是计算除每个 CxID 一行之外的所有行。您只需减去不同 CustomerID 的数量,即 count(DISTINCT cp.CxID)。所以你的最终查询应该很简单:

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

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

带限制的Mysql删除语句


来源:https://www.codenong.com/5004129/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9362.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?