【问题标题】:How can I check whether is NULL the result of JOIN?如何检查 NULL 是否是 JOIN 的结果?
【发布时间】:2017-12-14 23:36:05
【问题描述】:

这是我的表结构:

-- categories
+----+-------------+
| id |     name    |
+----+-------------+
| 1  | political   |
| 2  | cultural    |
| 3  | social      |
| 4  | historical  |
+----+-------------+

-- tags
+----+-------------+-----------+-------------+-----------------+
| id |     name    | parent_id | category_id |  total_used_num |
+----+-------------+-----------+-------------+-----------------+
| 1  | president   | NULL      | 1           | 43              |
| 2  | society     | NULL      | 3           | 345             |
| 3  | Trump       | 1         | 1           | 5               |
| 4  | book        | 5         | 2           | 5473            |
| 5  | library     | NULL      | 2           | 433             |
+----+-------------+-----------+-------------+-----------------+

这是我的查询:

SELECT t1.name, 
       CONCAT(t2.name, ',', cat.name) t_p 
FROM   tags t1 
       LEFT JOIN tags t2 
              ON t1.parent_id = t2.id 
       INNER JOIN categories cat 
               ON t1.category_id = cat.id 
ORDER  BY total_used_num DESC, 
          t1.id 

/* output
+----+-------------+--------------------------+
| 1  | president   | NULL                     |
| 2  | society     | NULL                     |
| 3  | Trump       | president,political      |
| 4  | book        | library,cultural         |
| 5  | library     | NULL                     |
+----+-------------+--------------------------+

看到了吗? CONCATNULL 的结果是 NULL。无论如何,我该如何避免呢?另外我想让逗号, 有条件。如果两个字段都不是NULL,那么逗号应该在那里。这是预期结果

+----+-------------+--------------------------+
| 1  | president   | political                |
| 2  | society     | social                   |
| 3  | Trump       | president,political      |
| 4  | book        | library,cultural         |
| 5  | library     | cultural                 |
+----+-------------+--------------------------+

我该怎么做?

【问题讨论】:

    标签: mysql sql null concat


    【解决方案1】:

    您可以使用 case 语句。这里是 Oracle 查询,你可以为 MySQL 修改它:

    SELECT   t1.name, 
                (case when t2.name is not null then ( t2.name || ', ') else '' end)  || cat.name 
    FROM   tags t1 
           LEFT JOIN tags t2 
                  ON t1.parent_id = t2.id 
           INNER JOIN categories cat 
                   ON t1.category_id = cat.id  
    ORDER  BY t1.total_used_num DESC, 
              t1.id ;
    

    【讨论】:

      【解决方案2】:

      只需使用 CASE WHEN 语句,即可获得预期的输出。 参考下面的代码。

      SELECT t1.name,  (CASE 
                      WHEN  (t2.name IS NULL) 
                      THEN  cat.name
                      ELSE (CONCAT(t2.name, ',', cat.name))
                      END) AS t_p 
      FROM   tags t1 
             LEFT JOIN tags t2 
                    ON t1.parent_id = t2.id 
             INNER JOIN categories cat 
                     ON t1.category_id = cat.id
      

      【讨论】:

        【解决方案3】:

        只需使用concat_ws():

        SELECT t1.name, 
               CONCAT_WS(',', t2.name, cat.name) as t_p 
        FROM tags t1 LEFT JOIN
             tags t2 
             ON t1.parent_id = t2.id INNER JOIN
             categories cat 
             ON t1.category_id = cat.id 
        ORDER BY total_used_num DESC, t1.id ;
        

        【讨论】:

        • 你到底是谁?您甚至在不到 1 分钟的时间内阅读了我的问题吗?!你真的很棒,很棒。我印象深刻。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-09-08
        • 2017-08-18
        • 1970-01-01
        • 2013-05-09
        • 2014-04-13
        相关资源
        最近更新 更多