mysql - How to add `select count` as a column -


i have table has invite_code , invited_by columns.

i select users , number of users invited user. done in 1 query?

e.g. have these data:

id | invite_code | invited_by    -----------------------------      1  |          11 |  2  |          22 |        11 3  |          33 |        11 4  |          44 |        22 

i add select count(*) users u.invite_code = invited_by result:

id | invite_code | invited_by | invite_num ------------------------------------------ 1  |          11 |            |         2 2  |          22 |         11 |         1 3  |          33 |         11 |         0 4  |          44 |         22 |         0 

i using mysql

try this:

select [id]       ,[invcode]       ,[invby], (select count(*) table t t.[invby]=t1.[invcode])   table  t1 

Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -