문제

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
도움이 되었습니까?

해결책

  • 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;
    

다른 팁

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top