SQL错误(1054)未知列
我这里有一个很长的sql查询SQL错误(1054)未知列,sql,Sql,我这里有一个很长的sql查询 SELECT c.clientid, c.clientname, c.billingdate, case when (select ifnull(sum(total), 0) from invoice i where i.client = c.clientid and i.isdeleted = 0) - (select ifnull(sum(p.amount), 0) from payment p inner join invoice i on p.invoic
SELECT c.clientid, c.clientname, c.billingdate,
case when (select ifnull(sum(total), 0) from invoice i
where i.client = c.clientid and i.isdeleted = 0) - (select ifnull(sum(p.amount), 0) from payment p inner join invoice i on p.invoice = i.invoiceid
where i.client = c.clientid and i.isdeleted = 0) < 0 and i.date < '2012-01-01' then (select ii.total from invoice ii where ii.client = c.clientid order by ii.invoiceid desc limit 1) else (select ifnull(sum(total), 0) from invoice i
where i.client = c.clientid and i.isdeleted = 0) - (select ifnull(sum(p.amount), 0) from payment p inner join invoice i on p.invoice = i.invoiceid
where i.client = c.clientid and i.isdeleted = 0) end as remaining,
case c.isactive+0
when '1' then 'Stop'
else 'Start'
end as Active
FROM client c
ORDER BY clientname
我在该查询中得到一个错误,行I.date<'2012-01-01'表示I.date是字段列表中的未知列…我如何解决此问题
谢谢,
J您在子查询中定义的范围外使用别名i
(select ifnull(sum(total), 0) from invoice i
where i.client = c.clientid and i.isdeleted = 0)
和
您应该包括以下内容:
and i.date < '2012-01-01'
在两个选择中
SELECT c.clientid, c.clientname, c.billingdate,
case when (select ifnull(sum(total), 0) from invoice i
where i.client = c.clientid and i.isdeleted = 0 and i.date < '2012-01-01' ) - (select ifnull(sum(p.amount), 0) from payment p inner join invoice i on p.invoice = i.invoiceid
where i.client = c.clientid and i.isdeleted = 0 and i.date < '2012-01-01' ) < 0 then (select ii.total from invoice ii where ii.client = c.clientid order by ii.invoiceid desc limit 1) else (select ifnull(sum(total), 0) from invoice i
where i.client = c.clientid and i.isdeleted = 0) - (select ifnull(sum(p.amount), 0) from payment p inner join invoice i on p.invoice = i.invoiceid
where i.client = c.clientid and i.isdeleted = 0) end as remaining,
case c.isactive+0
when '1' then 'Stop'
else 'Start'
end as Active
FROM client c
ORDER BY clientname
您在子查询中定义的范围外使用别名i
(select ifnull(sum(total), 0) from invoice i
where i.client = c.clientid and i.isdeleted = 0)
和
您应该包括以下内容:
and i.date < '2012-01-01'
在两个选择中
SELECT c.clientid, c.clientname, c.billingdate,
case when (select ifnull(sum(total), 0) from invoice i
where i.client = c.clientid and i.isdeleted = 0 and i.date < '2012-01-01' ) - (select ifnull(sum(p.amount), 0) from payment p inner join invoice i on p.invoice = i.invoiceid
where i.client = c.clientid and i.isdeleted = 0 and i.date < '2012-01-01' ) < 0 then (select ii.total from invoice ii where ii.client = c.clientid order by ii.invoiceid desc limit 1) else (select ifnull(sum(total), 0) from invoice i
where i.client = c.clientid and i.isdeleted = 0) - (select ifnull(sum(p.amount), 0) from payment p inner join invoice i on p.invoice = i.invoiceid
where i.client = c.clientid and i.isdeleted = 0) end as remaining,
case c.isactive+0
when '1' then 'Stop'
else 'Start'
end as Active
FROM client c
ORDER BY clientname
您是i.date在子查询之外。就在这里
SELECT c.clientid, c.clientname, c.billingdate,
CASE WHEN
(select ifnull(sum(total), 0) from invoice i where i.client = c.clientid and i.isdeleted = 0)
-
(select ifnull(sum(p.amount), 0) from payment p inner join invoice i on p.invoice = i.invoiceid where i.client = c.clientid and i.isdeleted = 0)
< 0
and i.date < '2012-01-01' -- THIS IS NOT PART OF THE SUBQUERY
THEN
(select ii.total from invoice ii where ii.client = c.clientid order by ii.invoiceid desc limit 1)
ELSE
(select ifnull(sum(total), 0) from invoice i where i.client = c.clientid and i.isdeleted = 0) - (select ifnull(sum(p.amount), 0) from payment p inner join invoice i on p.invoice = i.invoiceid where i.client = c.clientid and i.isdeleted = 0)
END as remaining,
case c.isactive+0
when '1' then 'Stop'
else 'Start'
end as Active
FROM client c
ORDER BY clientname
您是i.date在子查询之外。就在这里
SELECT c.clientid, c.clientname, c.billingdate,
CASE WHEN
(select ifnull(sum(total), 0) from invoice i where i.client = c.clientid and i.isdeleted = 0)
-
(select ifnull(sum(p.amount), 0) from payment p inner join invoice i on p.invoice = i.invoiceid where i.client = c.clientid and i.isdeleted = 0)
< 0
and i.date < '2012-01-01' -- THIS IS NOT PART OF THE SUBQUERY
THEN
(select ii.total from invoice ii where ii.client = c.clientid order by ii.invoiceid desc limit 1)
ELSE
(select ifnull(sum(total), 0) from invoice i where i.client = c.clientid and i.isdeleted = 0) - (select ifnull(sum(p.amount), 0) from payment p inner join invoice i on p.invoice = i.invoiceid where i.client = c.clientid and i.isdeleted = 0)
END as remaining,
case c.isactive+0
when '1' then 'Stop'
else 'Start'
end as Active
FROM client c
ORDER BY clientname
Cosidering date是大多数数据库中的保留字,您可能需要根据需要将其封装起来。。。[date]或带有勾号。在大多数数据库中,共注日期是保留字,您可能需要根据需要将其封装。。。[日期]或带有勾号。