即使列在MySQL中返回空结果,也会显示行

时间:2014-10-17 06:12:12

标签: mysql

以下是我的查询: -

SELECT u.name as title, 
COALESCE(f.field_full_name_value,'None Provided') as f.field_full_name_value,
u.mail, u.created, u.access, u.uid, COUNT(a.uid) AS times
FROM site_users u, site_node_view_count a, site_field_data_field_full_name f 
where a.uid = u.uid and f.entity_id = u.uid 
GROUP BY u.name
ORDER BY times desc

这样做是为了显示用户的网站访问详细信息。当我尝试将全名(f.field_full_name_value)显示为列时出现问题。如果某些用户没有输入全名(f.field_full_name_value),则不会显示整行。我希望无论在何处输入f.field_full_name_value,该字段都可以显示空字段,但仍显示其他字段的值。

我尝试使用COALESCE,如查询中所示,但它给了我错误。

有什么想法吗?

由于

3 个答案:

答案 0 :(得分:0)

使用此查询代替上述。

SELECT 
    u.name as title,
    u.mail,
    u.created,
    u.access,
    u.uid,
    COALESCE(f.field_full_name_value,'None Provided') AS field_full_name_value,
    COUNT(a.uid) AS times
FROM
    site_users u,
    site_node_view_count a,
    site_field_data_field_full_name f
WHERE
    a.uid = u.uid AND f.entity_id = u.uid
GROUP BY u.uid
ORDER BY times DESC

答案 1 :(得分:0)

我认为以下查询将返回您需要的结果

 SELECT u.name as title ,
    CASE
    WHEN f.field_full_name_value IS NULL
    THEN 'None Provided'
    ELSE  f.field_full_name_value
    END  as full_name_value, u.mail, u.created, u.access, u.uid, COUNT(a.uid) AS times
    FROM site_users u
    LEFT JOIN site_field_data_field_full_name f on u.uid =f.entity_id 
    INNER JOIN site_node_view_count a on a.uid = u.uid
    GROUP BY u.name
    ORDER BY times DESC

答案 2 :(得分:0)

使用OUTER JOIN,希望这有帮助。 。 。

SELECT u.name as title, 
COALESCE(f.field_full_name_value,'None Provided') as field_full_name_value,
u.mail, 
u.created, 
u.access, 
u.uid, 
COUNT(a.uid) AS times

FROM site_users u 

LEFT JOIN site_node_view_count a
ON a.uid = u.uid

LEFT OUTER JOIN site_field_data_field_full_name f 
ON f.entity_id = u.uid 

GROUP BY u.name
ORDER BY times desc