mysql - Query LEFT JOIN on condition -


let's have table of clients , table of "more_information".

i query list of clients , additional information (not every client has entries here, use left join):

 select id, name, more_information.data     clients     left join more_information     on more_information.client_id = clients.id 

now problem: want query "more_information" clients fulfill condition, f.e. signed in last 30 days, others want null more_information.data

so (syntax not correct)

 select id, name, more_information.data     clients     case when clients.registered > '2015-12-27 00:00:00'         left join more_information         on more_information.client_id = clients.id     else         null     end 

i know use case in select part, won't improve performance goal.

the reason more_information join queries lot of data , has many additional conditions make query slow. don't want query not necessary. possible mysql or need split query , make 2 separate queries?

thanks lot ideas

just add join condition

select id, name, more_information.data clients left join more_information on more_information.client_id = clients.id , clients.registered > '2015-12-27 00:00:00' 

an index on registered, id , client_id performance.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -