0

我在 PHP MYSQL 上有这个问题这是我的数据库示例

id word email
1  Helo jon@gmail.com
2  Sim  jon@gmail.com
3  Sam  jon@gmail.com
4  Mac  mars@gmail.com
5  Mimic mars@gmail.com

现在我在这里试图解决的是我如何计算具有相同电子邮件的电子邮件,例如 OUTPUT 网页:

id word email          submitted words dictionary
1  Helo jon@gmail.com        3         regular
2  Sim  jon@gmail.com        3         regular
3  Sam  jon@gmail.com        3         regular
4  Mac  mars@gmail.com       2         regular
5  Mimic mars@gmail.com      2         regular

提交的单词是电子邮件的计数。我怎样才能在php上计算出相同的电子邮件并输出3 ...

4

4 回答 4

1

使用此查询:

SELECT email, COUNT(*) AS nb_emails
FROM your_table
GROUP BY `email`

它将为您表中的每封电子邮件返回相关记录的数量。

如果您使用mysql_* 函数,您可以使用该代码获取所有值:

$sql = "SELECT email, COUNT(*) AS nb_emails
          FROM your_table
          GROUP BY `email`";
$res = mysql_query($sql);
while($row = mysql_fetch_assoc($res))
{
    // do something with the values of the last fetched record. The values are stored in $row['email'] and $row['nb_emails']
}

注意:不鼓励使用 MySQL 扩展。相反,应该使用MySQLiPDO_MySQL扩展。另请参阅MySQL:选择 API指南和相关的常见问题解答以获取更多信息。

于 2012-08-10T09:55:43.853 回答
0

用这个:

select email,
count(email) as count_same
from table_users
group by email
having ( count(email) > 1 );
于 2012-08-10T09:57:21.807 回答
0

使用mysql_num_rows()例如:

//mysql_connect() & mysql_select_db()    
$q=mysql_query("SELECT * FROM tablename WHERE email='jon@gmail.com'");
$count=mysql_num_rows($q); //3
于 2012-08-10T09:54:39.030 回答
0

尝试这个:

SELECT a.ID, a.Word, a.Email, b.SubmittedWords
FROM   myTable a INNER JOIN
        (
            SELECT email, COUNT(*) SubmittedWords
            FROM mytable
            GROUP BY email
        ) b
            ON a.email = b.email
于 2012-08-10T09:57:09.967 回答