繁体   English   中英

如何通过在codeigniter中使用权限指定联接值的最后一个字符来使用选择联接两个表?

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

我想使用select join与table_Atable_B并在table_A.in_no = table_B.in_no进行 连接 ,其中In_no是差值。

示例:Table_A中的In_no值= ISP1501-1

并且table_B中的In_no值= 1

因此,我想通过比较table_A中In_no的最后一个字符和它在图像上的表达式-1来加入in_no,但是我做不到。

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函数的View来获取所有数据

如果我将两个表中的In_no值更正为相同的值,它将起作用

在此处输入图片说明

所以我想问一下Stackoverflow中的所有成员。 有没有可能?

感谢您的帮助。

是的,您可以使用php字符串函数str_split()做到这一点...在这里,我向您证明了我的实例,希望对您有所帮助

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

您将需要这样的条件:

$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/zh-CN/string-functions.html#function_substring-index

您可能希望使用SUBSTRING_INDEX()在其他列(带有索引SUBSTRING_INDEX()中提取此值,以SUBSTRING_INDEX()条件并加快查询速度。

您可以使用子字符串

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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