简体   繁体   English

使用CodeIgniter在MySQL中区分大小写的LIKE和WHERE子句匹配

[英]Case sensitive LIKE and WHERE clause matching in MySQL using CodeIgniter

My database/table/column collation is set to *_ci mean case insensitive. 我的数据库/表/列排序规则设置为* _ci,表示不区分大小写。 What I want is to write case sensitive query on run time using CodeIgniter for LIKE and WHERE clause. 我想要的是使用CodeIgniter for LIKE和WHERE子句在运行时编写区分大小写的查询。 I tried an approach something like this 我尝试了这样的方法

$this->db->where("fieldname LIKE BINARY 'value'", NULL, true);

But this approach is not working with LIKE clause 但是这种方法不适用于LIKE子句

$this->db->like("fieldname LIKE BINARY 'value'", NULL);

Any type of help will greatly appreciated! 任何帮助将不胜感激!

How about this 这个怎么样

$value = "abdulla";
$query = $this->db->query("SELECT * FROM table WHERE fieldname1 Like '%$value%' OR fieldname2 Like '%$value%'");
$result = $query->result_array();

actually $this->db->like(); 实际上$this->db->like(); act as WHERE ??? LIKE '???' 充当WHERE ??? LIKE '???' WHERE ??? LIKE '???'

For example 例如

$this->db->like('name', 'abdulla');     
// Produces: WHERE name LIKE '%abdulla%'

You can do it with get() method like: 您可以使用get()方法执行此操作,例如:

$result = $this->db
    ->where('fieldname like binary "value"', NULL, FALSE)
    ->get('table')
    ->result();

So you can try it like as 所以你可以像这样尝试

$this->db->like('fieldname', 'value');

You can control the wildcard (%) as per your needs you can use 'before' , 'after' and 'both' . 您可以根据需要控制通配符(%) ,可以使用'before''after''both' If you don't need the wildcard then you can simply pass the third parameter as none . 如果您不需要通配符,则只需将第三个参数传递为none

Check the DOCS 检查DOCS

Well well well ... 好吧好吧...

After searching around and hammering my head I finally solved this issue! 经过四处搜寻并敲定我的头,我终于解决了这个问题! First of all I have to set the collation of table/column to *_cs, after that if I need case sensitive matching I have to run simple 首先,我必须将表/列的排序规则设置为* _cs,此后,如果需要区分大小写的匹配,则必须简单运行

$this->db->where('field', 'value');

or 要么

$this->db->like('field', 'value');

But if you want case insensitive matching than use 但是如果您要使用不区分大小写的匹配而不是使用

$this->db->where("LOWER('field')", strtolower('value'), false);

$this->db->where() accepts an optional third parameter. $ this-> db-> where()接受可选的第三个参数。 If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks. 如果将其设置为FALSE,则CodeIgniter不会尝试使用反引号保护您的字段或表名。

or 要么

$this->db->like("LOWER('field')", strtolower('value'));

Remember CodeIgniter follows the standard collation of your MySQL database, there is no built in function is available in CI 2.x to over write default database collation. 请记住,CodeIgniter遵循MySQL数据库的标准排序规则,CI 2.x中没有内置函数可用于覆盖默认数据库排序规则。 So if your column/table collation is set to *_ci (case insensitive) CI will consider abc = aBc but if your collation is set as *_cs it will consider abc != aBc 因此,如果您的列/表排序规则设置为* _ci(不区分大小写),CI将考虑abc = aBc,但是如果您的排序规则设置为* _cs,它将考虑abc!= aBc

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

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