mysql - Select items that doesn't have a specific value in another table -


i trying select items doesn't have specific value in table, able achieve result wanted using subquery, it's slow wondering if differently...

select     content.*,     (select views      content_views      content_views.content = content.record_num     ) views content right join watch_log on content.record_num = watch_log.content content.enabled = 1     , 24 not in         (select niche          content_niches          content_niches.content = content.record_num         ) order content.encoded_date desc limit 0,6 

i tried using left outer join, couldn't same result...

select     content.*,     (select content_views.views      content_views      content_views.content = content.record_num     ) views content right join watch_log on content.record_num = watch_log.content left outer join content_niches on content.record_num = content_niches.content , content_niches.niche = 24 content.enabled = 1 order content.encoded_date desc limit 0,6 

mixing left , right outer joins confusing. in fact, right join isn't needed. can replaced left join. in case, can replaced inner join, because where clause turns inner join. so, how about:

select c.*,        (select views         content_views cv         cv.content =  c.record_num        ) views content c join      watch_log wl      on c.record_num = wl.content c.enabled = 1 ,       not exists (select 1                   content_niches cn                   cn.content = c.record_num ,                         cn.niche = 24                  ) order c.encoded_date desc limit 0, 6; 

for performance want indexes: content(enabled, encoded_date, record_num), content_views(content, views), , content_niches(content, niche).

notes:

  • don't mix different types of outer joins, unless really, understand doing.
  • use table aliases abbreviations of table names. makes queries easier write , read.
  • whatever preference formatting, don't start line in query desc (or asc); modifier on order by.
  • not exists better not in. former handles null values way expect. latter returns nothing if there null values.

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -