开发者

SQLite select if results in other table are more than n

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜