YAN HONG YE <yanhong...@mpsa.com> wrote:
> select *  from hbc order by cmc desc limit 10
> union
> select * from hbc where qph>0
> union
> select * from hbc where hctl=1
> 
> this sql cmd cause the error:
> order by clause should come after union not before

select * from hbc where rowid in (select rowid from hbc order by cmc desc limit 
10)
union
select * from hbc where qph>0
union
select * from hbc where hctl=1;

A shorter, though not strictly equivalent, version:

select * from hbc where
    rowid in (select rowid from hbc order by cmc desc limit 10)
    or qph>0 or hctl=1;

If there are several identical rows in hdc, then the original version keeps 
just one copy of them, while this version may return more than one.

The third variant is equivalent to the second. Depending on which indexes you 
have on the table and which SQLite version you are using, it may be more 
efficient:

select * from hbc where rowid in (
    select rowid from (select rowid from hbc order by cmc desc limit 10)
    uinon
    select rowid from hbc where qph>0
    union
    select rowid from hbc where hctl=1
);

-- 
Igor Tandetnik

_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to