开发者

SQLite select if results in other table are more than n

开发者 https://www.devze.com 2023-03-05 19:30 出处:网络
I have the following tables in my database: sessionsmea开发者_JAVA百科surements --------------------

I have the following tables in my database:

sessions                 mea开发者_JAVA百科surements
--------                 ------------
id                       id
etc.                     session_id
                         etc.

I want to do something like

SELECT * FROM sessions IF 
   (measurements HAS MORE THAN 0 RESULTS WHERE session_id=X)

Is this sort of query possible in sqlite?

Best regards


Am not sure if SQLite supports EXISTS, so I've provided two alternatives:

SELECT *
FROM sessions S
WHERE EXISTS (SELECT 1 FROM measurements WHERE session_is = S.id)

OR

SELECT *
FROM sessions S
WHERE (SELECT COUNT(*) FROM measurements WHERE session_is = S.id) > 0


This will do it:

select * from sessions where (select count(*) from measurements where session_id=X)>0 
0

精彩评论

暂无评论...
验证码 换一张
取 消