MySQL总结了一些CASE WHEN函数

时间:2014-06-23 10:58:40

标签: mysql join case

是否有可能将三个CASE函数最小化为一个,而我期望任何CASE有三个不同的表达式?

因此所有CASE调用都依赖于一个值(con.category_id)。 我希望你能通过保留整个功能来帮助我清理这个SQL语句。

SELECT 
 UNIX_TIMESTAMP(con.content_date_publishing) AS time, 
 CASE 
  when con.category_id IS NOT NULL 
 THEN 
  con.category_id 
 ELSE 
  0 
 END AS category_id, 
 CASE 
  when con.category_id IS NOT NULL 
 THEN 
  cat.category_name_de 
 ELSE 
  NULL 
 END AS category_name 
FROM 
 v5_content AS con, 
 v5_category AS cat 
WHERE content_id = 2 AND 
 CASE 
  when con.category_id IS NOT NULL 
 THEN 
  con.category_id = cat.category_id 
 ELSE 
  1 

END 
LIMIT 
 1

1 个答案:

答案 0 :(得分:0)

您似乎错过了LEFT JOIN运营商。

SELECT
    UNIX_TIMESTAMP(con.content_date_publishing) AS time,
    cat.category_id,
    cat.category_name_de
FROM v5_content as con
LEFT JOIN v5 category as cat ON con.category_id = cat.category_id
WHERE
    content_id = 2;