我想看一个公民的全名
其中reportbasket、reportnobasket表中的notifyemployee=“1”
并且reportnobasket.groupemail不为空
在一个柜台上统计来自reportbasket、citizenemail reportnobasket的电子邮件
桌子:
公民(电子邮件、名字、姓氏)
报告篮(电子邮件,通知员工)
ReportNoBasket(公民电子邮件、通知员工、群发电子邮件)
在我以这种方式表示我的查询以显示他们的电子邮件之前,它工作得很好:

SELECT email,count(email) as email_count
FROM (
    SELECT email
    FROM reportBasket
    WHERE notifyEmployee='1'
    UNION ALL
    SELECT citizenEmail
    FROM reportNoBasket
    WHERE notifyEmployee='1'
) as T
GROUP BY email
ORDER BY email ASC

但现在我想显示公民的全名而不是电子邮件地址,例如concat(citizen.firstname,' ' citizen.lastname)
我怎样才能加入呢?

最佳答案

试试这样的:

SELECT concat(firstname, ' ', lastname), count(*) as citizen_count
FROM (
    SELECT c.firstname, c.lastname
    FROM reportBasket rb
    JOIN citizen c
        ON rb.email = c.email
    WHERE notifyEmployee='1'
    UNION ALL
    SELECT c.firstname, c.lastname
    FROM reportNoBasket rnb
    JOIN citizen c
        ON rnb.citizenEmail = c.email
    WHERE notifyEmployee='1'
) as T
GROUP BY firstname, lastname
ORDER BY firstname, lastname ASC

关于mysql - 如何在MySQL 2中联接多个表?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30936816/

10-11 22:37