mysql - Need Optimized Query which take data from three tables -


three tables

accounts, products, users 
  1. select users id , email users table there id in accounts
  2. then filter them out products table user bought more 3 products (need count users in product table , exclude user if count more 3).

       select id, email  users  id in ( select user_id accounts users.id = user_id ) 

i got users id , emails, need filter them out product table. in product table have 'product_id' , 'leader_id', user id.

i need optimized query, because tables big

i understand bit...something should works:

 select u.id,u.mail,  (select count(1)     products   products.leader_id = u.id)  conteo  user u   left join account ac  on(u.id = ac.user_id)  having (conteo>3) 

i hope helps ;)

saludos.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -