[英]Incorrect syntax near ')' in SQL
I am a newbie in SQL and I do not know what is wrong with this code : 我是SQL的新手,我不知道这段代码有什么问题:
select
t.title, pub.pub_name
from
titles t
inner join
publishers pub on t.title_id = pub.pub_id
where
t.title_id in (select title_id
from
(select t.title_id, sum(s.qty)
from titles t
inner join sales s on t.title_id = s.title_id
group by t.title_id
having sum(s.qty) > 20)
)
Do you folks know why this is not working? 你们知道为什么这行不通吗?
It seems that you are using SQL Server, and your error is because you don't use alias for your table in subquery. 似乎您正在使用SQL Server,并且出现错误是因为您没有在子查询中为表使用别名。 You could fix it by name an alias for it. 您可以通过为其命名为别名来修复它。
But IMO you should rewrite your query to this simple version: 但是IMO,您应该将查询重写为以下简单版本:
SELECT t.title , pub.pub_name
FROM titles t
INNER JOIN publishers pub
ON t.title_id = pub.pub_id
WHERE t.title_id IN (
SELECT title_id
FROM titles t
INNER JOIN sales s
ON t.title_id = s.title_id
GROUP BY t.title_id
HAVING SUM(s.qty) > 20
);
You should add alias to your Sub Query 您应该为您的子查询添加别名
select
t.title,
pub.pub_name
from
titles t
inner join publishers pub on t.title_id = pub.pub_id
where
t.title_id in (
select
title_id
from(
select
t.title_id,
sum(s.qty)
from
titles t
inner join sales s on t.title_id = s.title_id
group by
t.title_id
having
sum(s.qty) > 20
) as t_id
)
Or else it will throw an error at ) with something like this 否则它将在类似的地方引发错误)
Every derived table must have its own alias 每个派生表必须具有自己的别名
A good way to test your queries is to use validators online 测试查询的一种好方法是在线使用验证器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.