简体   繁体   English

SQL / SQlite嵌套select in having子句

[英]SQL / SQlite nested select in having clause

I was playing a bit with sqlite and encountered the following problem: it looks like it is not possible to nest a subquery in a having clause 我正在玩sqlite并遇到以下问题:看起来无法在子句中嵌套子查询

When I try to call a query with: 当我尝试使用以下命令调用查询时:

... having count(select * from produkt) > 1

I get the error: 我收到错误:

OperationalError: near "select": syntax error OperationalError:接近“select”:语法错误

While executing 执行时

 ... having count(1) > 1 

everything is fine 一切顺利

Would you have any workarounds for that? 你有任何解决方法吗?

edit2: EDIT2:

I want to write this: 我想写这个:

select distinct standort.name, standort.ort
from produktion
    join standort on id_standort = standort.id
    join produkt on id_produkt = produkt.id
where produkt.gewicht < 1

EXCEPT

select distinct standort.name, standort.ort
from produktion
    join standort on id_standort = standort.id
    join produkt on id_produkt = produkt.id
where produkt.kategorie = "Spiel"

In a more elegant way, using "having" 以更优雅的方式,使用“拥有”

Cheers and thanks a lot! 干杯,非常感谢!

Wojtek WOJTEK

I think this fits your intention better than your current select 我认为这比你当前的选择更符合你的意图

SELECT ... 
FROM Standort
     JOIN produktion ...
     JOIN produkt ...
WHERE product.kategorie != "Spiel" AND produkt.gewicht > 1

I'm not sure in what context you would do this, but this is syntactically correct: 我不确定你会在什么情况下这样做,但这在语法上是正确的:

having (select count(*) from produkt) > 1

EDIT: 编辑:

For your actual question, this query is simpler: 对于您的实际问题,此查询更简单:

select s.name, s.ort
from produktion pn join
     standort s
     on pn.id_standort = s.id join
     produkt p
     on pn.id_produkt = p.id
group by s.name, s.ort
having sum(case when p.gewicht < 1 then 1 else 0 end) > 0 and
       sum(case when p.kategorie = 'Spiel' then 1 else 0 end) = 0;

This returns all s.name / s.ort combinations that have a "gewicht" less than 1 and no "kategorie" called 'Spiel' . 这将返回“gewicht”小于1且没有“kategorie”称为'Spiel'所有s.name / s.ort组合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM