2012-02-03 41 views
0

我有一個複雜的查詢,這是再增加一個行前工作正常: 工作查詢:MySQL的語法錯誤

  $query = "SELECT SQL_CALC_FOUND_ROWS * 
        FROM realestate 
        INNER JOIN users ON id_user = users.id 
        LEFT JOIN pic ON id_realestate = realestate.id 
        group by realestate.id 
        ORDER BY {$order} 
        LIMIT 0 , 10"; 

$orderdate DESC

增加一個行後,這裏是查詢:

  $query = "SELECT SQL_CALC_FOUND_ROWS * 
        FROM realestate 
        {$place} 
        INNER JOIN users ON id_user = users.id 
        LEFT JOIN pic ON id_realestate = realestate.id 
        group by realestate.id 
        ORDER BY {$order} 
        LIMIT 0 , 10"; 

$place已將此值WHERE address =1字段名address在其他表中不重複。
我得到這個mysql錯誤:

Database query failed: 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 users ON id_user = users.id LEFT JOIN pic ON id_realestate = real' 
    at line 4 

回答

2

JOIN條款一定要來以前WHERE條款。你有錯誤的順序你的子句。

+0

你救了我的一天。非常感謝。 – 2012-02-03 01:52:51

1

根據http://dev.mysql.com/doc/refman/5.0/en/select.html

表引用而來的WHERE部分之前。下面列出的是正確的順序:

 $query = "SELECT SQL_CALC_FOUND_ROWS * 
       FROM realestate 
       INNER JOIN users ON id_user = users.id 
       LEFT JOIN pic ON id_realestate = realestate.id 
       {$place} 
       group by realestate.id 
       ORDER BY {$order} 
       LIMIT 0 , 10"; 

JOIN報表的編制是FROM表引用的一部分。