dongtui0650 2016-11-28 23:14
浏览 80
已采纳

MYSQL SELECT列中的唯一值,其中列2 =值,并计算每个唯一值的找到次数

I am looking to select and count each occurrence of column phone_number where column2 email_eddress == value.

Example DB Tables

Table: some_numbers

id | sender_email | phoner_number
1 | email@example.com | 555-555-5555
2 | email2@example.net | 666-666-6666
3 | email@example.com | 555-555-5555
4 | email@example.com | 123-456-7890

I am trying to select the phone_number value and the number of times each unique phone_number number was found WHERE sender_email = $email variable that is set to check an individual email address).

I can't seem to figure out how to query this in a way that I can just output something like shown below.

EXAMPLE OUTPUT

email@example.com | 555-555-5555 (2), 123-456-7890 (1)

email2@example.net | 666-666-6666 (1)

My Attempt:

$results = $db->query("SELECT DISTINCT phone_number FROM some_numbers GROUP BY sender_email");
    foreach($results as $result){
        $number = trim($result['phone_number']);

        echo $number.BR;
    }
  • 写回答

1条回答 默认 最新

  • dpk20361 2016-11-28 23:20
    关注

    Below query should work:

    select sender_email, phone_number, count(*)
    from some_numbers 
    group by sender_email, phone_number;
    

    Here is SQL Fiddle.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号