LIMIT & 问题IN/ALL/ANY/SOME 子查询
我有这个查询:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这就是您需要继续的方式。请参阅我已经完成的示例。
希望这有帮助。
This is how you need to proceed. See the example that I've worked out.
Hope this helps.
您不需要使用子查询来检索所有记录,只需排除第一个记录:
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
您还可以双重嵌套内部查询来绕过此限制,请参阅:
Mysql delete statements with limit
You can also double-nest the inner query to get around this restriction, see:
Mysql delete statement with limit
如果您想获得“每组的前 N 行”之类的信息,那么这种限制会很痛苦。但就你而言,即使可能,我也不会使用该功能。您尝试做的是计算除每个
CxID
一行之外的所有行。您只需减去不同 CustomerID 的数量,即count(DISTINCT cp.CxID)
。所以你的最终查询应该像这样简单: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 iscount(DISTINCT cp.CxID)
. So your final query should be as simple as: