Saiteja prasadam Saiteja prasadam - 6 months ago 26
SQL Question

SQLite subquerie 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)

Answer

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;