2015-01-16 41 views
1

我想用选择加入与TABLE_A表-B和加入table_A.in_no = table_B.in_no,这In_no的差值。如何使用右选择连接两个表来指定codeigniter中连接值的最后一个字符?

例子:在表-A In_no值= ISP1501-1

而在表-B In_no值= 1

所以我想加入就in_no在TABLE_A In_no的最后一个字符用其exsprestion相比-1就像我的图片一样,但我无法做到。

Codeigniter是否为这种情况提供了一些解决方案?

在我的模型

public function select_in_completed(){ 

      $this->db->select('add_val.*, invoice_all.*'); 
      $this->db->from('add_val'); 
      $this->db->join('invoice_all','invoice_all.in_no = add_val.in_no'); 
      $this->query = $this->db->get(); 
      if($this->query->num_rows()>0){ 
       return $this->query->result(); 
      } 
     } 

并在cotroller

public function index() { 

     if($this->logged_in ==TRUE){ 
      if($this->user_type == "editor"){ 

       $this->load->model('frontend/report_m'); 
       $this->data['user_id'] = $this->user_id; 
       $this->data['invoice'] = $this->report_m->select_in_completed(); 
       $this->data['subview'] = 'invoice/invoice'; 
       $this->load->view('frontend/report/complet/view_incom', $this->data); 
      }else{ 
      redirect(base_url('user/login')); 
     } 
    }else{ 
      redirect(base_url('user/login')); 
     } 
    } 

我使用视图与简单的代码的foreach函数来获取所有数据

如果我知道在两个表中的值相同In_no的矩形值,将工作

enter image description here

所以我想问一下在#1的所有成员。它可能与否?

感谢您的帮助。

回答

0

是的,你可以用PHP字符串函数做str_split() ...我在这里证明你的exampple我希望它会帮助你

$sql=mysql_fetch_assoc(mysql_query("select * from table1")); 
$table1=str_split($sql['in_no'],1); 

$sql2=mysql_fetch_assoc(mysql_query("select * from table2")); 
$table2=str_split($sql2[in_no],1); 

//here conditions go 
if($table1[9]==$table2[1]) 
{ 
//code runs 
} 
+0

我不明白你的变量$表[9] –

+0

对不起,我忘了给解释....这里从数据库中的字段日期是isp1501-39在那里权!!!!! ......当我们将分割最后一个值的时候会是这个数组[9] –

+0

我选择多于4000rows如果我用比较多的话会慢一点吧?我希望没有问题 –

0

你需要的条件是这样的:

$this->db->join('invoice_all',"invoice_all.in_no = SUBSTRING_INDEX(add_val.in_no, '-', -1)"); 

假设add_val是你TABLE_A

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substring-index

您可能想要在SUBSTRING_INDEX()的附加列(带有索引)中保留此值以使条件更容易,查询速度更快。

+0

我使用了 $ this-> db-> select('add_val。*,invoice_all。*'); \t \t $ this-> db-> from('add_val'); \t \t $ this-> db-> join('invoice_all','SUBSTRING_INDEX(invoice_all.in_no,' - ',-1)= add_val.in_no'); \t \t $ this-> query = $ this-> db-> get(); \t \t if($ this-> query-> num_rows()> 0){ \t \t \t return $ this-> query-> result(); \t \t} 但不能工作 –

+0

试试'$ this-> db-> join('invoice_all',“SUBSTRING_INDEX(invoice_all.in_no,' - ',-1)= add_val.in_no”);'否则你会从PHP得到解析错误,因为你有单引号单引号。 –

+0

我得到这个 错误号:在调用本地函数 'SUBSTRING_INDEX' SELECT * FROM('dbfinan_add_val')1582 不正确的参数计数JOIN'dbfinan_invoice_all' ON'SUBSTRING_INDEX'('dbfinan_invoice_all'.'in_no, ' - ','-1)= add_val.in_no 文件名:d:\ CooTel \ WAMP \ WWW \ Financial_Project \金融\ SYSTEM \数据库\ DB_driver.php 行号:330 –

0

您可以使用子

SUBSTRING('isp1501-32',8) gives 32 

public function select_in_completed(){ 

      $this->db->select('add_val.*, invoice_all.*'); 
      $this->db->from('add_val'); 
      $this->db->join('invoice_all','SUBSTRING(invoice_all.in_no,8) = add_val.in_no'); 
      $this->query = $this->db->get(); 
      if($this->query->num_rows()>0){ 
       return $this->query->result(); 
      } 
     } 
+0

我得到错误 您的SQL语法中有错误,您应该不会收到此错误;检查与您的MySQL服务器版本相对应的手册,在第3行的'8)= add_val.in_no'附近使用正确的语法 –

+0

您可以发布完整的SQL生成以上。 – vijaykumar

+0

现在我已经纠正它 –

相关问题