我在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 ...
答案 0 :(得分:1)
使用此查询:
SELECT email, COUNT(*) AS nb_emails
FROM your_table
GROUP BY `email`
它会为表格中的每封电子邮件返回相关记录的数量。
如果您使用的是mysql_* functions,则可以使用该代码获取所有值:
$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扩展。相反,应使用MySQLi或PDO_MySQL扩展名。有关详细信息,另请参阅MySQL: choosing an API指南和related FAQ。
答案 1 :(得分: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
答案 2 :(得分: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
答案 3 :(得分:0)
使用此:
select email,
count(email) as count_same
from table_users
group by email
having ( count(email) > 1 );