从codeigniter中的两个表中获取数据

时间:2017-04-03 09:34:43

标签: php sql codeigniter

我正在尝试执行查询以从表1获取数据并在表2中应用where子句 这是在Code-igniter上执行的示例查询

 SELECT * FROM table1,table2 WHERE `hide` = 0 AND `reject` = 0 AND `disable` = 0 AND `private` = 0 AND `table2`.`wepay_account_id` != '' ORDER BY `table1_id` DESC LIMIT 40

此查询在数据库中执行但不在代码点火器中执行

我在模型

中尝试此代码
 $this->db->limit($limit, $start);
        $this->db->where('hide', 0);
        $this->db->where('reject', 0);
        $this->db->where('disable', 0);
        $this->db->where('private', 0);
        $this->db->where('table2.wepay_account_id !=',"");
        $this->db->from('table1','table2');
        $this->db->order_by('table1', 'DESC');
        $query = $this->db->get();
        $a=$this->db->last_query();
        print_r($a);
        exit;

但我正面临这个错误

Error Number: 1054

Unknown column 'table2.wepay_account_id' in 'where clause'

SELECT * FROM (`table1`) WHERE `hide` = 0 AND `reject` = 0 AND `disable` = 0 AND `private` = 0 AND `table2`.`wepay_account_id` != '' ORDER BY `table1_id` DESC LIMIT 40

2 个答案:

答案 0 :(得分:2)

在此代码段中选择table1使用。没有table2

无法在table2 wepay_account_id添加条件。 $this->db->from('table1'); $this->db->join('table2', 'table1.id = table2.id')

使用此join(),

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if string.isEmpty {
        return true
    }
    let regex = "[a-z]{1,}"
    return NSPredicate(format: "SELF MATCHES %@", regex).evaluate(with: string)
}

答案 1 :(得分:0)

替换

$this->db->from('table1','table2');

$this->db->from('table1');
$this->db->join('table2','true');

这将是你的简单mysql查询的等价物。