如果附近有concat,以防万一没有工作

时间:2018-03-27 04:14:39

标签: mysql sql

我正在创建一个视图。以下是给我带来麻烦的。第一种情况正常。 217的第二个不起作用。我需要将CONCAT与WHERE一起使用。我尝试了很多方法,但无法让它发挥作用。是否有一种语法可以让我使用带有我使用

的concat的地方
(CASE  
    WHEN
      companies.mainSubcategory = '216'
    THEN
      CONCAT('{"label":"map","link":"https://www.google.com/maps?q=', 
             companies.street_number, ' ',
             companies.city , ' ',
             companies.state, '"}')
    WHEN 
       companies.mainSubcategory = '217'
    THEN
        CONCAT('{"label":"map","link":"https://www.google.com/maps?q=', 
               companies.street_number, ' ',
               companies.city , ' ',
                companies.state, '"}') WHERE
        companies.id = company_events.company_id)
   END) AS 'map1', 

我确实有连接但它们仅在mainSubcategory为216时才起作用。模式非常古怪,甚至不接近标准化。我不确定在尝试添加上面的WHERE时是否存在问题,或者它是否与连接到位的事实有关。

INNER JOIN company_event_associated_companies ON 
  company_event_associated_companies.event_id = 
  company_events.id
  AND company_events.start_date >= CURRENT_DATE

INNER JOIN companies ON
  company_event_associated_companies.company_id = 
  companies.id

1 个答案:

答案 0 :(得分:1)

WHERE表达式中间有一个CASE语句。它不属于。我想它应该是ON子句的一部分。

您应该使用表别名。 CASE应该看起来像:

(CASE WHEN c.mainSubcategory = 216
      THEN CONCAT('{"label":"map","link":"https://www.google.com/maps?q=', 
                  c.street_number, ' ',
                  c.city, ' ',
                  c.state, '"}'
                 )
      WHEN c.mainSubcategory = 217
      THEN CONCAT('{"label":"map","link":"https://www.google.com/maps?q=', 
                  c.street_number, ' ',
                  c.city , ' ',
                  c.state, '"}')
  END) as map1, 

如果您想在CASE表达式中进行额外过滤,请将其添加到条件:

(CASE WHEN c.mainSubcategory = 216 AND c.id = ce.company_id
      THEN CONCAT('{"label":"map","link":"https://www.google.com/maps?q=', 
                  c.street_number, ' ',
                  c.city, ' ',
                  c.state, '"}'
                 )
      WHEN c.mainSubcategory = 217 AND c.id = ce.company_id
      THEN CONCAT('{"label":"map","link":"https://www.google.com/maps?q=', 
                  c.street_number, ' ',
                  c.city , ' ',
                  c.state, '"}')
  END) as map1,