简体   繁体   English

使用CASE和INNER JOIN的MySQL查询错误

[英]MySQL query error with CASE and INNER JOIN

I have a table with user info called 'a' and a table with data from different API's (twitter,foursquare) which in the example, based on the value of the a.api_type should become b. 我有一个包含名为“ a”的用户信息的表,以及一个包含来自不同API(twitter,foursquare)的数据的表,在该示例中,基于a.api_type的值应变为b。 What I want in the end is to be able to grab the right avatar from the active API (either api_foursquare or api_twitter). 我最后想要的是能够从活动的API(api_foursquare或api_twitter)中获取正确的头像。

I have been trying to get this to work with this query for a while, but I keep getting this error. 我一直在尝试使它与此查询配合使用一段时间,但我一直收到此错误。 Sql is not my strongest point, so any tips on how to fix this would be great :) Sql不是我的长处,所以任何有关解决此问题的技巧都将是不错的:)

  "[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near

  INNER JOIN b ON b.user_id = a.user_id  at line 11"

SELECT a.user_id, 
       a.api_type, 
       b.avatar, 
       b.user_id
(CASE
       WHEN a.api_type = 0 THEN api_foursquare
       WHEN a.api_type = 1 THEN api_twitter
END) as b
FROM a WHERE a.cookie_hash = :cookie_hash
INNER JOIN b ON b.user_id = a.user_id

You cannot pick a table to join in a case statement. 您不能选择要加入案例陈述的表。 You should left-join to both tables, and then pick the value from one of them in the case statement, like this: 您应该左联接两个表,然后在case语句中从其中一个表中选择值,如下所示:

SELECT a.user_id, 
       a.api_type,
       (case WHEN a.api_type = 0 THEN b.avatar ELSE c.avatar END) as avatar,
       (case WHEN a.api_type = 0 THEN b.user_id ELSE c.user_id END) as user_id
FROM a 
  LEFT OUTER JOIN api_foursquare b ON b.user_id = a.user_id
  LEFT OUTER JOIN api_twitter c ON c.user_id = a.user_id
WHERE a.cookie_hash = :cookie_hash

You probably do not need the last expression (the ...as user_id one), because it is going to be equal to a.user_id if there is a row in either api_twitter or api_foursquare that matches a.user_id . 你可能并不需要最后的表情( ...as user_id之一),因为这将是等于a.user_id如果在任一排api_twitterapi_foursquare匹配a.user_id

You also have to put the WHERE clause after the FROM clause: 您还必须将WHERE子句放在FROM子句之后:

EDIT: Taking into account ypercube's great suggestion, the query would look like this: 编辑:考虑到ypercube的伟大建议,查询将如下所示:

SELECT a.user_id, 
       a.api_type,
       COALESCE(b.avatar, c.avatar) as avatar
FROM a 
  LEFT OUTER JOIN api_foursquare b ON b.user_id = a.user_id AND a.api_type = 0
  LEFT OUTER JOIN api_twitter c ON c.user_id = a.user_id and a.api_type = 1
WHERE a.cookie_hash = :cookie_hash

You need comma after b.user_id and before (CASE : 您需要在b.user_id之后和之前加逗号(CASE

SELECT a.user_id, 
    a.api_type, 
    b.avatar, 
    b.user_id,
    (CASE
           WHEN a.api_type = 0 THEN api_foursquare
           WHEN a.api_type = 1 THEN api_twitter
    END) as b
FROM a WHERE a.cookie_hash = :cookie_hash
    INNER JOIN b ON b.user_id = a.user_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM