内部连接两个表时语法错误

时间:2012-03-15 17:00:50

标签: mysql

我正在尝试加入表活动场地,但是在内部加入时我遇到了一个问题。我就这样做了:

$query =    "SELECT e.*, v.* FROM events e WHERE start_datetime >= '$DATE_START_SELECTED' AND end_datetime < '$DATE_END_SELECTED'".
           "INNER JOIN venues v ON e.VENUE_LOCATION = v.VENUE_ID";

由于某种原因,我有这样的错误:

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   在'INNER JOIN场地附近v ON e.VENUE_LOCATION = v.VENUE_ID'在第1行

知道为什么吗?谢谢!


修改

我将代码更新为:

$query =    "SELECT e.*, v.* FROM events e ".
        "INNER JOIN venues v ON e.VENUE_LOCATION = v.VENUE_ID".
        "WHERE start_datetime >= '$DATE_START_SELECTED' AND end_datetime < '$DATE_END_SELECTED'";

只是为了得到这个错误:

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   'start_datetime&gt; ='2012-03-15 06:00:00'AND_datetime&lt;   '2012-03-16 05:59:00''在第1行

3 个答案:

答案 0 :(得分:8)

您的INNER JOIN需要在WHERE条款之前。

如,

select e.*, v.*
from events e
inner join venues v on e.VENUE_LOCATION = v.VENUE_ID
where start_datetime >= '$DATE_START_SELECTED' 
    and end_datetime < '$DATE_END_SELECTED'

答案 1 :(得分:1)

加入始终在from之后和where之前。因此,您的查询将更像下面的内容:

SELECT e.*, v.* 
FROM events e 
    INNER JOIN venues v ON e.VENUE_LOCATION = v.VENUE_ID
WHERE start_datetime >= '$DATE_START_SELECTED' 
    AND end_datetime < '$DATE_END_SELECTED'

答案 2 :(得分:1)

在结尾处写下where子句(我还会扩展速记符号,例如“FROM events e to FROM events AS e:

SELECT e.*, v.* FROM events AS e 
INNER JOIN venues AS v ON e.VENUE_LOCATION = v.VENUE_ID
WHERE e.start_datetime >= yourVar AND e.end_datetime < yourOtherVar;