【问题标题】:SQL Query for string with difference of only one characterSQL查询字符串只有一个字符的差异
【发布时间】:2019-11-09 23:37:10
【问题描述】:

我正在尝试使用 Laravel 进行查询,但我认为我不能使用 Eloquent,所以我必须使用原始查询。

在 BD 中,我有一个字符串 name 列,我想在进行查询之前删除关于冒号的字符串。字符串为My name is: John

我正在以where('name', $name) 的身份进行查询,但在我的变量$name 中没有冒号。就像$name = My name is John

我试图做一个REPLACE,但我得到了空

查询是

select * from `table` where REPLACE(`name`, ':', '') = 'My name is John' limit 1

口才

Model::whereRaw("REPLACE(`name`, ':', '') = '?'", $name)->limit(1)->first()

我不想使用sounds like,因为我有其他类似的寄存器,并且根据我的经验,这部分的声音非常开放。

有什么建议吗?

【问题讨论】:

  • select replace('My name is: John', ':', '') = 'My name is John' 确实评估为真,如this db fiddle所示
  • 您的查询本身没有问题dbfiddle.uk/…
  • 我刚刚在 MySQL 中直接测试了它,如果它也可以工作。我正在尝试用 Laravel 来做,但它似乎不起作用。

标签: mysql sql laravel eloquent


【解决方案1】:

编辑:

DB::raw()where() 的第一项上,第二项是比较,第三项是过滤器。

原文:

您可以在您的位置上使用DB::raw()。使用 eloquent,您可以执行以下操作:

$filter = 'My name is john';
$result = DB::table('table')->where(DB::raw("replace(name,':', '')"), '=', $filter)->get();

DB::raw() 函数会将确切的术语传递给查询

【讨论】:

  • 最后的咨询是这样的$result = DB::table('table')->where(DB::raw("replace(name,':', '')"), '=', $name)->first()
猜你喜欢
  • 2016-12-09
  • 2018-10-09
  • 1970-01-01
  • 2014-10-02
  • 1970-01-01
  • 2012-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多