ds34222 2012-07-16 11:28
浏览 33
已采纳

按平台选择和计算不同的活动用户,并比较它们是否存在于其他表中

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
    
  • 写回答

3条回答 默认 最新

  • dongle19863 2012-07-18 10:38
    关注

    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'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大