计算左连接的结果只返回1行

时间:2016-12-06 15:36:18

标签: mysql sql

我有一个包含用户的表,另一个表包含他们提交的网站。我想选择所有用户,无论他们是否在网站表中都有网站。如果他们这样做,我想计算那些在我的PHP脚本中使用的那些。

这是我的问题:

SELECT 
    u.user_country, 
    u.user_id, 
    u.user_email, 
    u.user_verified, 
    c.country_iso, 
    c.country_nicename,  
    count(distinct w.website_id) as websitecount
FROM user u
LEFT JOIN country c
    ON c.country_id = u.user_country
LEFT JOIN websites w
    ON w.website_owner = u.user_id 

我的用户表:

CREATE TABLE `user` (
  `user_id` bigint(20) NOT NULL,
  `user_username` varchar(50) NOT NULL,
  `user_email` varchar(255) NOT NULL,
  `user_firstname` varchar(50) NOT NULL,
  `user_surname` varchar(50) NOT NULL,
  `user_country` varchar(100) NOT NULL,
  `user_password` varchar(255) NOT NULL,
  `user_verified` varchar(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

我的网站表

CREATE TABLE `websites` (
  `website_id` bigint(20) NOT NULL,
  `website_name` varchar(75) NOT NULL,
  `website_url` varchar(255) NOT NULL,
  `website_type` varchar(10) NOT NULL,
  `website_verified` varchar(10) NOT NULL,
  `website_owner` bigint(20) NOT NULL,
  `website_adult_content` varchar(3) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

现在,当我从SQL中删除count(distinct w.website_id) as websitecount时,它会按原样选择所有行。但有了它,它只选择1.我试图选择所有。

2 个答案:

答案 0 :(得分:0)

您需要在此使用group by

SELECT 
    u.user_country, 
    u.user_id, 
    u.user_email, 
    u.user_verified, 
    c.country_iso, 
    c.country_nicename,  
    count(distinct w.website_id) as websitecount
FROM user u
LEFT JOIN country c
    ON c.country_id = u.user_country
LEFT JOIN websites w
    ON w.website_owner = u.user_id 
group by u.user_country, 
    u.user_id, 
    u.user_email, 
    u.user_verified, 
    c.country_iso, 
    c.country_nicename

如果您没有包含group by子句并且您正在使用聚合函数,MySQL将任意选择一条记录返回。一个group by基本上说“给我这些东西的每一个组合”,如果你没有,MySQL不知道你想要什么,但也没有告诉你。如果您没有group by,其他RDBMS甚至不允许您运行查询,但出于某种原因,如果您未在查询中包含此子句,MySQL可以选择是否要提醒您。有关详细信息,请参阅http://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html

答案 1 :(得分:0)

看起来你错过了分组。您需要在计数中给出相同的列。

SELECT 
    u.user_country, 
    u.user_id, 
    u.user_email, 
    u.user_verified, 
    c.country_iso, 
    c.country_nicename,  
    count(w.website_id) as websitecount
FROM user u
LEFT JOIN country c
    ON c.country_id = u.user_country
LEFT JOIN websites w
    ON w.website_owner = u.user_id
GROUP BY 
    u.user_country, 
    u.user_id, 
    u.user_email, 
    u.user_verified, 
    c.country_iso, 
    c.country_nicename

注意:如果您需要不同的网站数量,上述查询应该是好的

如果您要查找所有网站计数(即重复项),则需要删除不同的

CREATE Procedure user_password_date_interval_check
    @ua_pk uniqueidentifier
AS
    DECLARE @PasswordExpireDays int
    SET @PasswordExpireDays = 0

    SELECT
        sm_Setting, sm_Value
    FROM
        Setting_Misc AS sm
    INNER JOIN
        Syndicates As syn ON sm.syn_fk = syn.syn_pk
    INNER JOIN
        Company As c ON c.syn_fk = syn.syn_pk
    INNER JOIN
        User_Accounts As ua ON ua.c_fk = c.c_pk
    WHERE 
        sm.sm_Setting = 'PasswordExpireDays'
        THEN sm.sm_Value = @PasswordExpireDays