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. 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).
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 🙂
"[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
casestatement, like this:You probably do not need the last expression (the
...as user_idone), because it is going to be equal toa.user_idif there is a row in eitherapi_twitterorapi_foursquarethat matchesa.user_id.You also have to put the
WHEREclause after theFROMclause:EDIT: Taking into account ypercube’s great suggestion, the query would look like this: