Question

I have this query and appearently it's faulty? I'm trying to join fices with mems so I can have the ficeID along with all the results from mems(These queries work individually). What am I doing wrong?

  SELECT * 
    FROM mems 
   WHERE deleted <> -1 
ORDER BY sort_mems 
         LEFT JOIN SELECT ficeID 
                     FROM fices

Result:

 #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 'LEFT JOIN SELECT ficeID FROM offices LIMIT 0, 30' at line 1
Was it helpful?

Solution

  • You have JOIN clause after ORDER BY. You should place it in FROM.
  • I suggest you define condition of a LEFT JOIN
  • Also, I suggest you surround you temp tables with brackets:

      SELECT m.*, t1.officeID
        FROM members m
             LEFT JOIN offices t1
                       ON m.memberID = t1.memberID
       WHERE m.deleted <> -1 
    ORDER BY m.sort_membername;
    

OTHER TIPS

Yes, you have the LEFT JOIN in the wrong spot (it should go after your FROM clause, and you also seem to be missing a join criteria (the ON part, this tells the database how these tables are related):

SELECT *
FROM mems m
LEFT JOIN fices f
  ON  m.??? = f.???
WHERE deleted <> -1 
ORDER BY sort_mems
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top