我有一个表的用户信息被称为'a'和一个表来自不同API(twitter,foursquare)的数据,在这个例子中,基于a.api_type的值 应该成为b。我最终想要的是能够从活动API(api_foursquare或api_twitter)中获取正确的头像。CASE和INNER JOIN的MySQL查询错误
我一直在试图让这个使用这个查询一段时间,但我不断收到此错误。 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
我会在第一个'ON'条件(和第二个'= 1')中添加'AND a.api_type = 0'。 – 2012-01-08 12:02:13
然后你可以使用'COALESCE(b.avatar,c.avatar)AS avatar',等等 – 2012-01-08 12:03:33
@ypercube很棒的建议!我编辑了答案来加入它。我认为查询更具可读性,并且为更多api类型添加表也更加容易。谢谢! – dasblinkenlight 2012-01-08 12:09:56