Why does SELECT results differ between mysql and sqlite?

前端 未结 3 1342
你的背包
你的背包 2021-02-12 13:41

I\'m re-asking this question in a simplified and expanded manner.

Consider these sql statements:

create table foo (id INT, score INT);

insert into foo v         


        
3条回答
  •  面向向阳花
    2021-02-12 14:21

    I tried to mess with some variants of query.

    It seems, like sqlite has errors in using of previous declared fields in a nested HAVING expressions.

    In your example avg1 under second having is always equal to 5.0

    Look:

    select T1.id, avg(T1.score) avg1
    from foo T1
    group by T1.id
    having not exists (
        SELECT 1 AS col1 GROUP BY col1 HAVING avg1 = 5.0);
    

    This one returns nothing, but execution of the following query returns both records:

    ...
    having not exists (
        SELECT 1 AS col1 GROUP BY col1 HAVING avg1 <> 5.0);
    

    I can not find any similar bug at sqlite tickets list.

提交回复
热议问题