sql - Select all entities which do not have children -- getting a lot of rows back, is this correct? -


i've got 84,000 rows in users table. users created automatically. so, thought nice see how many users did after being created. wrote query:

select count(*) users u join folders f on userid = u.id join playlists p on folderid = f.id 0 = (select count(*) playlistitems playlistid = p.id)  

my intent count users have no playlist items in of playlists. query returned 74,000 results seems high.

i'm wondering if query selecting users have @ least 1 playlist no items in it. is, if user has 2 playlists -- 1 empty , 1 populated -- still counted in query? and, if so, how can modify select users have empty playlists.

if that's vastly more difficult might try hand @ counting users 1 playlist empty.

the database structure is:

  • many users. 1:1 user:folder, 1:many folder:playlists, 1:many playlists:playlistitems

a better pattern counting every single playlist , comparing finding users don't have in playlist. not exists this:

select count(u.id)   dbo.users u   not exists    (     select 1 dbo.playlists pl      inner join dbo.playlistitems pli      on pl.id = pli.playlistid      inner join dbo.folders f      on p.folderid = f.id      f.userid = u.id   ); 

as aside, calling column id in primary table , else everywhere else might seem idea, find quite confusing. why isn't folderid called folderid everywhere in data model?


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -