【问题标题】:mysql error: an alias was previously foundmysql错误:以前找到了一个别名
【发布时间】:2016-11-27 12:23:46
【问题描述】:

我在 mysql 查询中使用这些代码

    select c1.date,sum(SELECT IFNULL(cx.purchase-cx.sold,0))+MAX(p.openqty) as opening,c1.purchase,c1.sold,
    sum(SELECT IFNULL(cx.purchase-cx.sold,0))+MAX(p.openqty) + c1.purchase-c1.sold as closing 
from 
    (select open_qty as openqty from stock ) p
left join
(select IFNULL(a.date,b.date) as date,SELECT IFNULL(a.qty,0) as purchase,SELECT IFNULL(b.qty,0) as sold from arrival a FULL JOIN pouring b ON a.date = b.date order by 1) c1 
on c1.purchase +p.openqty>0
left join
 (select IFNULL(a.date,b.date) as date,SELECT IFNULL(a.qty,0) as purchase,SELECT IFNULL(b.qty,0) as sold from arrival a FULL JOIN pouring b ON a.date = b.date order by 1) cx 
on c1.date>cx.date
 group by c1.date,c1.purchase,c1.sold

上面写着:

An alias was previously found. (near "purchase" at position 170)
An alias was previously found. (near "c1" at position 179)
An alias was previously found. (near "sold" at position 182)
An alias was previously found. (near "closing" at position 190)

MySQL 说:文档

1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册以了解正确的语法使用

'SELECT IFNULL(cx.purchase-cx.sold,0))+MAX(p.openqty) 附近为 开幕,c1.purchase,c1。在第 1 行

请帮忙如何克服?

【问题讨论】:

  • 您有两次完全相同的子查询,两次都使用相同的变量名。你试过改变它们吗?
  • sum(SELECT IFNULL(cx.purchase-cx.sold,0)) 更改为(SELECT sum(cx.purchase-cx.sold)。明白了吗?
  • 先生我改成:select c1.date, SELECT sum(cx.purchase-cx.sold)+MAX(p.openqty) as opening,c1.purchase,c1.sold, sum(SELECT IFNULL(cx.purchase-cx.sold,0))+MAX(p.openqty) + c1.purchase-c1.sold as close from (select open_qty as openqty from stock ) p 但还是一样的错误

标签: mysql


【解决方案1】:

首先,子查询需要有自己的括号。其次,子查询不能作为聚合函数的参数。第三,您根本不需要子查询。所以:

select c1.date,
       (COALESCE(sum(cx.purchase-cx.sold), 0) + MAX(p.openqty)
       ) as opening, c1.purchase, c1.sold,
       (COALESCE(sum(cx.purchase-cx.sold), 0) + MAX(p.openqty) + c1.purchase - c1.sold
       ) as closing 

【讨论】:

  • #1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取正确的语法,以便在 ' 0)) + MAX(p.openqty) + c1.purchase - c1.sold ) 附近使用 (se' at line 4
  • 现在这里是第二部分)作为关闭(选择 open_qty 作为 openqty from stock)p 左连接(选择 IFNULL(a.date,b.date) 作为日期,SELECT IFNULL(a.qty, 0) 作为购买,SELECT IFNULL(b.qty,0) 从到货开始销售 a FULL JOIN 倾倒 b ON a.date = b.date order by 1) c1 on c1.purchase +p.openqty>0 left join (select IFNULL(a.date,b.date) 作为日期,选择 IFNULL(a.qty,0) 作为购买,选择 IFNULL(b.qty,0) 作为从到达开始销售 a.date = b 的 FULL JOIN 浇注 b。 date order by 1) cx on c1.date>cx.date group by c1.date,c1.purchase,c1.sold
  • MySQL 说:文档 #1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取正确的语法,以便在第 9 行的“SELECT IFNULL(a.qty,0) as purchase,SELECT IFNULL(b.qty,0) as sold from paid a”附近使用跨度>
  • @user3003813 。 . .鉴于我的回答没有使用IFNULL(),完全不清楚这会如何产生该错误。
【解决方案2】:

一种可能性

select c1.date,sum(coalesce(cx.purchase-cx.sold,0))+MAX(p.openqty)  as opening,
c1.purchase,
c1.sold,
sum(coalesce(cx.purchase-cx.sold,0)) + MAX(p.openqty) + c1.purchase-c1.sold as closing 
from ( select open_qty as openqty from stock ) p
left join ( select coalesce(a.date,b.date) as date,
            coalesce(a.qty,0) as purchase,
            coalesce(b.qty,0) as sold 
            from arrival a 
            LEFT JOIN pouring b ON a.date = b.date 
            UNION select coalesce(a.date,b.date) as date,
            coalesce(a.qty,0) as purchase,
            coalesce(b.qty,0) as sold 
            from arrival a 
            RIGHT JOIN pouring b ON a.date = b.date 
            order by 1) c1 on c1.purchase +p.openqty>0
left join ( select coalesce(a.date,b.date) as date,
            coalesce(a.qty,0) as purchase,
            coalesce(b.qty,0) as sold 
            from arrival a 
            LEFT JOIN pouring b ON a.date = b.date 
            UNION select coalesce(a.date,b.date) as date,
            coalesce(a.qty,0) as purchase,
            coalesce(b.qty,0) as sold 
            from arrival a 
            RIGHT JOIN pouring b ON a.date = b.date order by 1) cx on c1.date>cx.date
group by c1.date,c1.purchase,c1.sold 
order by 1

【讨论】:

    猜你喜欢
    • 2018-10-01
    • 2017-07-05
    • 2018-01-20
    • 1970-01-01
    • 2022-06-14
    • 2022-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多