简体   繁体   English

Drupal 7 - 使用db_select不区分大小写LIKE

[英]Drupal 7 - case-insensitive LIKE with db_select

I can't get a query to be case-insensitive using MySQL and Drupal 7. Here's my module code: 我无法使用MySQL和Drupal 7获得不区分大小写的查询。这是我的模块代码:

$results = db_select('people_table', 'p')->fields('p');
if (array_key_exists('department', $_GET)) {
    $results->condition('Department', '%' . db_like($_GET['department']) . '%', 'LIKE');
}
return $results->orderBy('Name', 'ASC')->execute();

With ?department=Chemistry in the URL, I get three results. 使用URL中的?department=Chemistry ,我得到三个结果。 With ?department=chemistry in the URL, I get no results. 使用URL中的?department=chemistry ,我没有得到任何结果。 When I try $results->condition('UPPER(Department)'... , I get this error: 当我尝试$results->condition('UPPER(Department)'... ,我收到此错误:

PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'UPPERDepartment' in 'where clause': SELECT p.* FROM {people_table} p WHERE (UPPERDepartment LIKE :db_condition_placeholder_0 ESCAPE '\\\\') ORDER BY Name ASC; PDOException:SQLSTATE [42S22]:找不到列:1054'where子句'中的未知列'UPPERDepartment':SELECT p。* FROM {people_table} p WHERE(UPPERDepartment LIKE:db_condition_placeholder_0 ESCAPE'\\\\')ORDER BY Name ASC;

So it looks like it eats my parentheses. 所以看起来它吃掉了我的括号。 How can I do a case-insensitive LIKE ? 我怎么能做一个不区分大小写的LIKE

Edit: the collation on the Department column as well as on the whole table is utf8_bin . 编辑: Department列和整个表上的utf8_binutf8_bin This answer says "The only special one is utf8_bin which is for comparing characters in binary format." 这个答案说“唯一特别的是utf8_bin,用于比较二进制格式的字符。” I don't know why this collation was chosen, since all the data in the table is English text. 我不知道为什么选择这种排序规则,因为表中的所有数据都是英文文本。 I might just change the collation to utf8_general_ci . 我可能只是将整理更改为utf8_general_ci

什么是Department字段的排序规则?,对于ex utf8_unicode_ci,它应该是前缀* _ci(不区分大小写)

您可以使用where方法添加自定义WHERE子句,而不是使用condition

$results->where('UPPER(Department)'...

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

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