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

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;
    }

图片转代码服务由CSDN问答提供 功能建议

我希望选择并计算列phone_number的每个匹配项,其中column2 email_eddress == value。 \ n

示例数据库表

表: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 
   
 
 

我正在尝试选择phone_number值以及找到每个唯一phone_number编号的次数WHERE sender_email = $设置的电子邮件变量 检查单个电子邮件地址。)

我似乎无法弄清楚如何以一种我可以输出如下所示的方式来查询它。

示例输出

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

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

我的尝试:

  $ results = $ db-&gt  ; 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.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题