简体   繁体   中英

How to use select join two table by using right to specify last character of join values in codeigniter?

I want to used select join with table_A and table_B and join on table_A.in_no = table_B.in_no , which In_no are difference value.

Example: In_no value in Table_A = ISP1501-1

And In_no value in table_B = 1

So I want to join on in_no by compared on last character of In_no in table_A with its exsprestion -1 as on my images But I can't do it.

Does Codeigniter have some solution for this case ?

In my model

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();
            }
        }

And In 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'));
        }
    }

And I used View with simple code foreach function to fetch all data

And if I correct value of In_no in in both table to the same value it will work

在此处输入图片说明

So I would like to ask all member in Stackoverflow. it is possible or not?

Thanks for helping us.

Yes you can do it with php string function str_split() ...here i am proving you the exampple i hope it will helps you

$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
 }

You'll need condition like this:

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

assuming add_val is your table_A

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

You might want to keep this value extracted with SUBSTRING_INDEX() in additional column (with an index on it) to make the condition easier and queries faster.

You can use substring

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();
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM