我的MySQLi查询给出了错误,但我无法发现它的错误

时间:2014-08-31 14:05:25

标签: php mysqli

我正在使用mysql查询使用LEFT OUTER JOIN从多个表中选择数据。现在,当我执行查询时,我收到以下错误:

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   靠近' wg.werkbon_global_id = wk.werkbon_klant_globalid LEFT OUTER   加入用户AS u'在第16行

唯一的问题是我无法找出查询的错误。

PHP查询:

$query = '
    SELECT
        wg.werkbon_global_id AS id,
        wg.werkbon_global_status AS status,
        wg.werkbon_global_date_lastedit AS date,
        usr.user_firstname AS monteur_vn,
        usr.user_insertion AS monteur_tv,
        usr.user_lastname AS monteur_an,
        wg.werkbon_global_type AS type,
        wg.werkbon_global_layout AS layout,
        wg.werkbon_global_werkzaamheden AS werkzaamheden,
        wg.werkbon_global_opmerkingen AS opmerkingen,
        wk.werkbon_klant_nummer AS klantnr
    FROM
        werkbon_klant AS wk
    LEFT OUTER JOIN werkbon_global AS wg
        wg.werkbon_global_id = wk.werkbon_klant_globalid
    LEFT OUTER JOIN users AS usr
        usr.user_id = wg.werkbon_global_monteur_finish
    WHERE
        wk.werkbon_klant_nummer = '.$db->Quote($klantid).'
    ORDER BY id ASC;
$result = $db->loadAssoc($query);

我认为我的问题与左外连接有什么结果但是什么?

3 个答案:

答案 0 :(得分:3)

您缺少连接中的ON运算符!

连接的正确语法是:

SELECT * FROM x LEFT JOIN y ON condition WHERE...

答案 1 :(得分:0)

 $query = "
 SELECT
    wg.werkbon_global_id AS id,
    wg.werkbon_global_status AS status,
    wg.werkbon_global_date_lastedit AS date,
    usr.user_firstname AS monteur_vn,
    usr.user_insertion AS monteur_tv,
    usr.user_lastname AS monteur_an,
    wg.werkbon_global_type AS type,
    wg.werkbon_global_layout AS layout,
    wg.werkbon_global_werkzaamheden AS werkzaamheden,
    wg.werkbon_global_opmerkingen AS opmerkingen,
    wk.werkbon_klant_nummer AS klantnr
FROM
    werkbon_klant AS wk
LEFT OUTER JOIN werkbon_global AS wg
    wg.werkbon_global_id = wk.werkbon_klant_globalid
LEFT OUTER JOIN users AS usr
    usr.user_id = wg.werkbon_global_monteur_finish
WHERE
    wk.werkbon_klant_nummer = '.$db->Quote($klantid).'
ORDER BY id ASC";

$ result = $ db-> loadAssoc($ query);

确保没有错过报价

答案 2 :(得分:0)

感谢arkascha

引发的问题

现在修复查询:

$query = '
    SELECT
        wg.werkbon_global_id AS id,
        wg.werkbon_global_status AS status,
        wg.werkbon_global_date_lastedit AS date,
        usr.user_firstname AS monteur_vn,
        usr.user_insertion AS monteur_tv,
        usr.user_lastname AS monteur_an,
        wg.werkbon_global_type AS type,
        wg.werkbon_global_layout AS layout,
        wg.werkbon_global_werkzaamheden AS werkzaamheden,
        wg.werkbon_global_opmerkingen AS opmerkingen,
        wk.werkbon_klant_nummer AS klantnr
    FROM
        werkbon_klant AS wk
    LEFT OUTER JOIN werkbon_global AS wg ON
        wg.werkbon_global_id = wk.werkbon_klant_globalid
    LEFT OUTER JOIN users AS usr ON
        usr.user_id = wg.werkbon_global_monteur_finish
    WHERE
        wk.werkbon_klant_nummer = '.$db->Quote($klantid).'
    ORDER BY id ASC';
$result = $db->loadAssoc($query);

@fred我不需要按列名添加引号。您只需要按字符串/ blob值添加引号。 @johny我的$ db-> Quote()函数会自动添加qoutes。我不需要添加它们并将所有内容放在引号中。

谢谢大家的帮助。