“#1054 - 未知列”在查询中使用别名时出错?

发布于 2024-12-10 08:26:49 字数 760 浏览 0 评论 0原文

使用以下查询时, 出现错误

#1054 - Unknown column 'plus' in 'field list'

使用 plus-minus 时 。否则,查询运行良好。我想有一些与别名相关的东西我不知道如何使用。请指导!

谢谢。

询问:

SELECT users.name,
count(*) as total, 
SUM(sms.views)+ SUM(sms.downloads)+ (SELECT count(*) FROM `smsfb` WHERE (`feedback`=1 OR `feedback`=100) AND userid=users.uniqueID) AS plus,
SUM(sms.delreq)+(SELECT count(*) FROM `smsfb` WHERE (`feedback`=5 OR `feedback`=6) AND userid=users.uniqueID) AS minus,
plus-minus
FROM sms,users
WHERE sms.deviceID=users.uniqueID AND sms.catid!=23 AND sms.catid!=44 AND sms.catid!=45
AND date>="2011-10-03" AND date<"2011-10-09" 
GROUP BY users.uniqueID HAVING total>10 ORDER BY total DESC LIMIT 0, 10

While using the following query,
I get the error

#1054 - Unknown column 'plus' in 'field list'

when plus-minus is used. Otherwise, query runs fine. I guess there is something related to Aliases which I do not know how to use. Please guide!

Thanks.

Query:

SELECT users.name,
count(*) as total, 
SUM(sms.views)+ SUM(sms.downloads)+ (SELECT count(*) FROM `smsfb` WHERE (`feedback`=1 OR `feedback`=100) AND userid=users.uniqueID) AS plus,
SUM(sms.delreq)+(SELECT count(*) FROM `smsfb` WHERE (`feedback`=5 OR `feedback`=6) AND userid=users.uniqueID) AS minus,
plus-minus
FROM sms,users
WHERE sms.deviceID=users.uniqueID AND sms.catid!=23 AND sms.catid!=44 AND sms.catid!=45
AND date>="2011-10-03" AND date<"2011-10-09" 
GROUP BY users.uniqueID HAVING total>10 ORDER BY total DESC LIMIT 0, 10

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

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

发布评论

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

评论(1

放赐 2024-12-17 08:26:49

您不能在查询的选择部分中使用列的别名。

您可以这样做:

SELECT name
     , total
     , plus
     , minus
     , plus - minus
 FROM (
    SELECT users.name,
    count(*) as total, 
    SUM(sms.views)+ SUM(sms.downloads)+ (SELECT count(*) FROM `smsfb` WHERE     (`feedback`=1 OR     `feedback`=100) AND userid=users.uniqueID) AS plus,
    SUM(sms.delreq)+(SELECT count(*) FROM `smsfb` WHERE (`feedback`=5 OR `feedback`=6)     AND     userid=users.uniqueID) AS minus
    FROM sms,users
    WHERE sms.deviceID=users.uniqueID
      AND sms.catid!=23 AND sms.catid!=44
      AND sms.catid!=45
      AND date>="2011-10-03" AND date<"2011-10-09" 
    GROUP BY users.uniqueID HAVING total>10 ORDER BY total DESC
    LIMIT 0, 10
 ) plusAndMinus

来自列别名问题

您可以在 GROUP BY、ORDER BY 或 HAVING 子句中使用别名来引用列

You can't use the alias of a column inside the select part of the query.

You could do it in this way:

SELECT name
     , total
     , plus
     , minus
     , plus - minus
 FROM (
    SELECT users.name,
    count(*) as total, 
    SUM(sms.views)+ SUM(sms.downloads)+ (SELECT count(*) FROM `smsfb` WHERE     (`feedback`=1 OR     `feedback`=100) AND userid=users.uniqueID) AS plus,
    SUM(sms.delreq)+(SELECT count(*) FROM `smsfb` WHERE (`feedback`=5 OR `feedback`=6)     AND     userid=users.uniqueID) AS minus
    FROM sms,users
    WHERE sms.deviceID=users.uniqueID
      AND sms.catid!=23 AND sms.catid!=44
      AND sms.catid!=45
      AND date>="2011-10-03" AND date<"2011-10-09" 
    GROUP BY users.uniqueID HAVING total>10 ORDER BY total DESC
    LIMIT 0, 10
 ) plusAndMinus

From Problems with Column Aliases:

You can use the alias in GROUP BY, ORDER BY, or HAVING clauses to refer to the column

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