【问题标题】:Query Join On two table fail at error code 1064在两个表上查询连接失败,错误代码为 1064
【发布时间】:2012-03-04 13:04:04
【问题描述】:

我通过使用获取约束记录上的行数

$table1="tab1";
        $table2="tab2";
$query=sprintf("SELECT '%s'.* FROM '%s' JOIN '%s' ON ('%s'.id='%s'.id)",
               $table1,
               $table1,
               $table2,
               $table1,
               $table2);
        $query=$this->db->query($query);
        return $query->num_rows();

我想出了以下错误,我正在使用最新的 XAMPP 和 mysql 版本 5.1

错误号:1064

您的 SQL 语法有错误;检查手册 对应于您的 MySQL 服务器版本,以便使用正确的语法 靠近'.* FROM 'tab1' JOIN 'tab2' ON ('tab1'.userid='tab2'.userid)' 在第 1 行

SELECT 'tab1'.* FROM 'tab1' 加入 'tab2' ON ('tab1'.userid='tab2'.userid)

文件名:A:\CodeIgniter_2.1.0\system\database\DB_driver.php

行号:330

更新

$query=sprintf("SELECT `%s`.* FROM `%s` JOIN `%s` ON (`%s`.id=`%s`.id) LIMTI `%d`, `%d`",
                   $table1,
                   $table1,
                   $table2,
                   $table1,
                   $table2,
                   $num1,
                   $num2);

错误是

错误号:1327

未声明的变量:5

选择tab1.* FROM tab1 加入tab2 ON (tab1.userid=tab2.userid) 限制5,0

文件名:A:\CodeIgniter_2.1.0\system\database\DB_driver.php

行号:330

【问题讨论】:

  • 在表名周围使用反引号 (`) 而不是撇号。撇号用于 MySQL 中的字符串值。
  • 它有效,谢谢 Farray。

标签: php mysql codeigniter xampp


【解决方案1】:

试试这个代码。

$table1="tab1";
        $table2="tab2";
$query=sprintf("SELECT `%s`.* FROM `%s` JOIN `%s` ON (`%s`.id=`%s`.id)",
               $table1,
               $table1,
               $table2,
               $table1,
               $table2);
        $query=$this->db->query($query);
        return $query->num_rows();

我认为您的第二个查询应该是这样的。

$query=sprintf("SELECT `%s`.* FROM `%s` JOIN `%s` ON (`%s`.id=`%s`.id) LIMTI %d, %d",
                   $table1,
                   $table1,
                   $table2,
                   $table1,
                   $table2,
                   $num1,
                   $num2);

【讨论】:

  • 谢谢,但我现在坚持第二个查询。
  • 请查看我的答案底部。我也为您的第二个查询添加了修复程序。
【解决方案2】:

MySQL 需要用反引号引用表和列标识符,而不是单引号:

$query=sprintf("SELECT `%s`.* FROM `%s` JOIN `%s` ON (`%s`.id=`%s`.id)",
           $table1,
           $table1,
           $table2,
           $table1,
           $table2);

【讨论】:

    猜你喜欢
    • 2017-04-08
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-28
    相关资源
    最近更新 更多