简体   繁体   中英

SQLite subquery select statement

I have two tables lets assume history and Favorites

  • Favorites schema is (id int primary key, isFav int)
  • History schema is (id int)

Favorites table has => songs with id and an int (0 or 1) and history table has => series of id which may be duplicates

I want a SQL statement for selecting ids from favorites which has value = 1 and order by number of occurrence(count) in history table (it may contain or not record with that id)

This should work for you:

SELECT
x.id
, (
  SELECT 
     COUNT(y.id) 
  FROM y 
  WHERE y.id = x.id
  ) as YCount
FROM x
WHERE value = 1
ORDER BY  YCount;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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