2012-02-13 37 views
-1

我用查询联接在两条表失败的错误代码1064

$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语法错误;在第1行检查手册 对应于您的MySQL服务器版本的正确语法在'。* FROM'tab1'JOIN'tab2'ON ('tab1'.userid ='tab2'.userid)'附近使用

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

文件名:A:\ CodeIgniter_2.1.0 \ SYSTEM \数据库\ DB_driver .PHP

行号:330

UPDATE

$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

SELECT tab1 * FROM tab1 JOIN tab2 ON (tab1 .userid = tab2 .userid)LIMIT 50

文件名:A:\ CodeIgniter_2.1.0 \ SYSTEM \数据库\ DB_driver.php

行号:330

+0

在表名称周围使用反引号(')而不是撇号。撇号用于MySQL中的字符串值。 – Farray 2012-02-13 03:41:51

+0

它的工作原理,谢谢Farray。 – user1125233 2012-02-13 03:51:55

回答

0

试试这个代码。

$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); 
+0

谢谢,但我坚持在第二个查询现在。 – user1125233 2012-02-13 03:51:43

+0

请检查我的答案的底部。我也为您的第二个查询添加了修复程序。 – 2012-02-13 03:57:58

0

在MySQL中期望与反引号引用表和列的标识符,而不是单引号:

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