php - ->where() 子句似乎区分大小写

标签 php mysql laravel-5 where-clause fluent

我有一个这样的查询:

$profilesx->where('qZipCode', $location)->
                        orWhere('qCity', 'LIKE', '%'.$location.'%');

位置等于贝尔格莱德,数据库列显示贝尔格莱德

它似乎区分大小写(使用 = 或 LIKE),因此如果我搜索 Belgrade 我会得到结果,但如果我搜索 belgrade 我不会得到结果任何结果。

如何使其不区分大小写?

最佳答案

The default character set and collation are latin1 and latin1_swedish_ci, so nonbinary string comparisons are case insensitive by default. This means that if you search with col_name LIKE 'a%', you get all column values that start with A or a. To make this search case sensitive, make sure that one of the operands has a case sensitive or binary collation. For example, if you are comparing a column and a string that both have the latin1 character set, you can use the COLLATE operator to cause either operand to have the latin1_general_cs or latin1_bin collation:

来源:http://dev.mysql.com/doc/refman/5.7/en/case-sensitivity.html

实际发生的情况是区分大小写已被关闭(这不一定是坏事)。该文档的下一部分给出了解决方案。尝试类似的事情

orWhere('qCity', 'COLLATE latin1_general_cs LIKE', '%'.$location.'%');

如果 laravel 不喜欢它,您将不得不使用原始查询或更改列的排序规则设置。

顺便说一句,如果可以的话,请尽量避免 LIKE %something% 查询。 Mysql 无法对此类查询使用索引,因此它们在大型表上通常会很慢。

关于php - ->where() 子句似乎区分大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38523383/

相关文章:

php - 函数 getName 在 symfony 2 形式中做什么

php - 从 "kartik-v/yii2-detail-view"v1.7.7 更新 "v1.7.6 to "后出现错误

mysql - 如何在 MYSQL 中的两个日期(从日期,到日期)表之间获取数据?

php - 如何修复在 Laravel 中找不到的类 'App\Http\Controllers\Notification'?

php - 数据库事务在 laravel 5.2 中不起作用?

php - MySQL 时间戳和 PHP

php - 如何使用php关闭mysql中的dbconnection

mysql - 在 MySQL 中仅选择仅包含字母数字字符的行

java - 在 Spring-Boot 中处理并发事务

php - Schema::dropIfExists() 在迁移时删除了数据库中的所有表:回滚