sql - CASE switch inner join table mysql -


so have table contains edits on 3 other tables, , need switch inner join depending on value, ce.entity_id. yes, have normalized edits content_edits table, seemed lot easier.

        select ce.id, ce.file, ce.date_init, ce.content_type, ce.created_by_id, ce.status, ce.date_decide, u.username                          content_edits ce                          inner join users u                          on ce.created_by_id=u.id                              case ce.entity_type                                 when ce.entity_type=0                                   inner join m_articles m                                   on ce.entity_id=markers.id                                 when 1                                   inner join groups g                                   on ce.entity_id=groups.id                                 when 3                                   inner join e_news e                                   on ce.entity_id=events.id                          ce.status=1                          limit 10 

sincere help. appreciated.

the join cannot dependent on case. can either union 3 tables in inner query , use them query below

select ce.id, ce.file, ce.date_init, ce.content_type, ce.created_by_id, ce.status, ce.date_decide, u.username,s.details                          content_edits ce                          inner join users u                          on ce.created_by_id=u.id                          inner join (                            /*add details required columns*/                            select 0 entity_type,a.id entity_id,a.art_details details                             m_articles                             union                             select 1 entity_type,g.id entity_id,g.grp_details details                             groups g                            union                             select 3 entity_type,n.id entity_id,n.n_details details                             e_news n                            )s                                   on ce.entity_id=s.entity_id                                   , ce.entity_type = s.entity_type                          limit 10; 

http://sqlfiddle.com/#!9/2b984/1


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -