Codeigniter喜欢通配符

时间:2015-10-09 17:55:37

标签: php sql codeigniter

当我想在Codeigniter中写下以下行时:

SELECT * FROM Customers WHERE City LIKE '%es%'; // CONTAINS 'es'

我写这段代码:

$this->db->select('*');
$this->db->from('Customers');
$this->db->like('City', 'es');

您能否帮我写下以下一行的相关代码:

SELECT * FROM Customers WHERE City LIKE 'ber%'; // STARTS WITH 'ber'

这样它将显示以'ber'开头的城市的信息

1 个答案:

答案 0 :(得分:5)

您可以使用第三个参数来定义通配符的位置:

$this->db->like('City', 'es', 'after');
//SELECT * FROM Customers WHERE City LIKE 'ber%'

$this->db->like('City', 'es', 'before');
//SELECT * FROM Customers WHERE City LIKE '%ber'

$this->db->like('City', 'es', 'both'); //Default
//SELECT * FROM Customers WHERE City LIKE '%ber%'