SQL - 获取表1的所有行和行数

时间:2015-07-09 18:58:08

标签: mysql sql count

如何创建一个SQL查询,该查询获取表格的所有行和在电子邮件下插入的行数?

我尝试过类似的东西,但这会对行进行分组,因此我无法获取所有行。

SELECT *, COUNT(email) AS 'count' FROM adverts GROUP BY email

2 个答案:

答案 0 :(得分:2)

select a1.*, a2.count
from adverts a1
join
(
  SELECT email, COUNT(*) AS 'count' 
  FROM adverts 
  GROUP BY email
) a2 on a1.email = a2.email

答案 1 :(得分:1)

试试这个,

select *,
    (select count(Email) 
    from adverts  where adverts.Email =a.Email) as EmailCount
    from adverts as a

或者

SELECT *, COUNT(email) OVER (PARTITION BY  email) as EmailCount FROM adverts