简体   繁体   中英

Select and count distinct active users by platform and compare if they exist in other tables

I have about six(6) tables each linked with userid. One of the tables is userinfo. The user info contains user details including their store platform(eg magento)

  1. Userinfo contains both active and non-active users (active users have created at least one activity in the other 5 tables).
  2. I want to count distinct number of users in the userinfo with platform of magento who have records in any of the other tables.
  3. Currently I am able to count distinct number of users in the other five tables with the ff code but want to join this with the userinfo table so I can select active users with platform magento.
  4. Without adding the userinfo table means I have no way of selecting users by platform.
  5. Selecting users in userinfo table only, with platform of magento will be easy, but that means I may select users who only register but do not go on to create activity on my app.

      $query3 = ("SELECT COUNT(*) FROM ( SELECT userid FROM table1 UNION SELECT userid FROM table2 UNION SELECT userid FROM table3 UNION SELECT userid FROM table4 UNION SELECT userid FROM table5 ) AS UserIDs"); $result3 = mysql_query($query3) or die(mysql_error()); $row3 = mysql_fetch_row($result3); echo "Number of distinct users in all tables = ".$row3[0] ."<br />"; **Table 1** Id userid name adresss **Table 2** Id Title Sex userid **Table 3** Id userid amount **Table 4** Id price promotion userid productid **Table 5** Id userid category tax weight **userinfo** Id userid username password platform 

Expanding on the UNION subselect from my other suggestion , you can JOIN this with the UserInfo table and get your distinct count.

SELECT   COUNT (DISTINCT ui.UserID))
FROM     (
           SELECT UserID FROM Table1
           UNION SELECT UserID FROM Table2
           UNION SELECT UserID FROM Table3
           UNION SELECT UserID FROM Table4
           UNION SELECT UserID FROM Table5
         ) AS id
         INNER JOIN UserInfo ui ON ui.UserID = id.UserID
WHERE    ui.Platform = 'Magento'

I would like that :

SELECT COUNT(DISTINCT ui.userid) as number
FROM userinfo ui
INNER JOIN table1 t1 ON (t1.userid = ui.userid) 
INNER JOIN table2 t2 ON (t2.userid = ui.userid) 
INNER JOIN table3 t3 ON (t3.userid = ui.userid) 
INNER JOIN table4 t4 ON (t4.userid = ui.userid) 
INNER JOIN table5 t5 ON (t5.userid = ui.userid) 
WHERE ui.platform = 'magento'

And if you do :

    SELECT COUNT(DISTINCT ui.userid) as number
    FROM userinfo ui, table1 t1, table2 t2, table3 t3, table4 t4, table5 t5
    WHERE ui.platform = 'magento'
    AND t1.userid = ui.userid
    AND t2.userid = ui.userid 
    AND t3.userid = ui.userid 
    AND t4.userid = ui.userid
    AND t5.userid = ui.userid

If it doesn't work, try to replace SELECT COUNT(DISTINCT ui.userid) as number by SELECT ui.* for see.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM