EDITED:
请将table_row
视为table_column
(Dudu Markovitz)
我有一个值的子集,我想针对SQL中的一行进行评估。
Select count(*) cnt
from table_name c
where c.table_row in ('value 1','value 2', 'value 3')
and other statements;
只要我只想确定" table_row"包含这三个值中的一个。
然而,我想要确定table_row是否包含仅这些值。
是否有任何SQL语句可以执行仅包含的' in'陈述,以便对于" cnt"在上面的查询中,我只会得到一个结果,如果' table_row'在查询指示的集合中?
答案 0 :(得分:0)
也许会这样做?
select count(*) cnt
from table_name c
where
not exists (
select * table_name t
where COALESCE(table_row,'-1') != 'value 1'
and COALESCE(table_row,'-1') != 'value 2'
and COALESCE(table_row,'-1') != 'value 3'
)
and other statements;
看起来很奇怪,因为子查询没有链接到父查询但是......它应该可以工作。
答案 1 :(得分:0)
您可以使用Having子句。下面是伪代码。
SELECT Employees.LastName, COUNT(Orders.OrderID) AS NumberOfOrders FROM Orders
INNER JOIN Employees
ON Orders.EmployeeID=Employees.EmployeeID
GROUP BY LastName
HAVING LastName in ('Davolio','Fuller')
答案 2 :(得分:0)
这是你可能想要的 -
仅在不包含其他值的情况下显示计数结果,然后输入值1','值2'和'值3'
select count(*) as cnt
from table_name c
where c.mycol in ('value 1','value 2', 'value 3')
-- ...and other statements
having count(*) =
count(case when c.table_row in ('value 1','value 2', 'value 3') then 1 end)
你可能也想看看计数
select count(*) as cnt
,count(case when c.table_row in ('value 1','value 2', 'value 3') then 1 end) as cnt_val
from table_name c
where c.mycol in ('value 1','value 2', 'value 3')
-- ...and other statements