SQL Error (1064)

This query fails: I am trying to SELECT columns from different tables, I know left-join, inner join etc. But this is different.

SELECT stream.user_id, stream.verb, stream.object_id
(SELECT users.id, users.uid FROM users
    (SELECT profiles.user_id, profiles.name, profiles.picture, profiles.link FROM profiles
    WHERE profiles.user_id = users.uid) 
    WHERE users.id = "1")
FROM stream
INNER JOIN follows ON stream.user_id = follows.following_user
WHERE follows.user_id = "0"
ORDER BY stream.stream_date DESC LIMIT 10;

You’ve got some messed up syntax there.

  • You’re missing commas (first one is at the sub query)
  • have errors in your sub-queries.
  • you’re referencing a table (follows) which you never actually retrieve from

What exactly are you trying to accomplish? If you can tell me that, and what fields you’re actually trying to retrieve, I can help you straighten out the queries…

1 Like

I am trying to select different columns from different tables, and then I set a condition, that is using the follows table with the INNER JOIN.

OK, so item #3 was wrong (I’m doing a ton all at once). First two are definitely wrong - what fields are you looking for from users and profiles? Those are the ones that are hosing you…

1 Like

(SELECT users.id, users.uid FROM users
(SELECT profiles.user_id, profiles.name, profiles.picture, profiles.link FROM profiles
WHERE profiles.user_id = users.uid)

OK, that part would tie together as

SELECT users.id
     , users.uid 
     , profiles.user_id
     , profiles.name
     , profiles.picture
     , profiles.link 
  FROM users 
 INNER JOIN profiles ON profiles.user_id = users.uid
 WHERE users.id = "1"

How does that tie to the main query? In your sub-query, you’re looking for user id 1, and the main as 0.

1 Like
SELECT stream.user_id, stream.verb, stream.object_id
(SELECT users.id
     , users.uid 
     , profiles.user_id
     , profiles.name
     , profiles.picture
     , profiles.link 
  FROM users 
 INNER JOIN profiles ON profiles.user_id = users.uid
 WHERE users.id = "1") 
FROM stream
INNER JOIN follows ON stream.user_id = follows.following_user
WHERE follows.user_id = "0"
ORDER BY stream.stream_date DESC LIMIT 10;

EDIT: It still doesn’t work properly.

Again, that was only part of the problem. This query is syntactically correct, so it’ll run, but the sub-query really doesn’t make sense in this context. Why would you look for a user_id of 0 in the main and user_id of 1 in the sub-query.

 SELECT stream.user_id
      , stream.verb
      , stream.object_id
      , SQ.id
      , SQ.uid 
      , SQ.user_id
      , SQ.name
      , SQ.picture
      , SQ.link 	
   FROM stream
  INNER JOIN follows ON stream.user_id = follows.following_user
  INNER JOIN (SELECT users.id
		   , users.uid 
		   , profiles.user_id
		   , profiles.name
		   , profiles.picture
		   , profiles.link 
	        FROM users 
	       INNER JOIN profiles ON profiles.user_id = users.uid
	       WHERE users.id = "1") SQ
  WHERE follows.user_id = "0"
  ORDER BY stream.stream_date 
  DESC LIMIT 10;

Thanks a lot. I have modified it further to cover my needs.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.