我想显示标题包含单词" on "one_answers" ol "的剧集的数量。
从表中使用CTE:- table
我尝试了下面的代码,但它抛出错误:
select
count (select Title from tblEpisode
where Title like '%on%' or Title like '%ol%')
from tblEpisode
错误:
Msg 156, Level 15, State 1, Line 1
关键字'select'附近语法错误。Msg 102,第15层,状态1,第1行
')'附近语法错误。
有什么问题吗?请帮助!
似乎您根本不需要这里的子查询。向外部查询添加WHERE
:
SELECT COUNT(*)
FROM dbo.tblEpisode
WHERE Title LIKE '%on%'
or Title LIKE '%ol%';
有什么问题?
对于每一行count
期望*
或标量表达式,并将计算标量表达式为not null
的行。在SQL中,表达式不是标量,而是子选择。
[I]如何使用[a] CTE?
一些可能性:所有这些人为的例子使代码比没有CTE更糟糕。
WITH filtered AS (SELECT *
FROM dbo.tblEpisode
WHERE Title LIKE '%on%'
OR Title LIKE '%ol%')
SELECT COUNT(*)
FROM filtered;
WITH getCount AS (SELECT Count(*) AS CountStar
FROM dbo.tblEpisode
WHERE Title LIKE '%on%'
or Title LIKE '%ol%')
SELECT *
FROM getCount;
WITH cte AS (SELECT *
FROM dbo.tblEpisode)
SELECT COUNT(*)
FROM cte
WHERE Title LIKE '%on%'
or Title LIKE '%ol%';
回到你的SQL基础…
select -- the data you want
from -- the relevant tables
where -- the conditions you want to apply
- 您要选择什么?行数。
count(*)
- 相关表格是什么?
dbo.tblEpisode
- 您想申请什么条件?
title like '%on%' or title like '%ol%'
select count(*)
from dbo.tblEpisode
where Title like '%on%' or Title like '%ol%';