mysql - Get rows with condition from 2 tables -


i need top 3 referrers along number of referrals qualified ones (table_2). hope makes sense. far have following query. please help. thank you

select count(*) total_referrals, referrer table_1 table_2.qualified = '1' group referrer order total_referrals desc  limit 0,3 

table_1

referrer   referral user1      user89 user1      user54 user1      user23 user1      user56 user2      user89 user2      user23 user2      user45 user3      user78 user3      user14 user4      user10 user5      user98 user5      user56 ... 

table_2

referral   qualified user89       1 user54       0 user23       0 user56       1 user89       1 user23       1 user45       0 user78       1 user14       1 user10       0 user98       1 user56       1 ... 

you need reference both tables in clause , decide whether field name referrer or username:

select count(table_1.referrals) total_referrals, referrer table_1 left join table_2 on table_1.referrals=table_2.referrals table_2.qualified = '1' group table_1.referrer order total_referrals desc  limit 0,3 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -