【问题标题】:Best way to avoid specific values, when extracting from database?从数据库中提取时避免特定值的最佳方法是什么?
【发布时间】:2021-01-30 07:08:35
【问题描述】:

问题

忽略特定值,当使用 medoo 拉取数组并合并时。

细节

假设我有两个数组,由两个相似的表创建。他们有一个“用户”行和一个“id”行。 下面的例子:

id        name   
-------   ---------
  1         John Doe     
  2         John Snow    
  3         Jane     
  4         Jane       

忽略名称行中包含“John ...”的所有条目的最佳方法是什么? 我的 array_merge 目前是这样的:

  public function getUsersByName($name)
    {
        $res = array_merge(
            $this->_db->select('users1', ['id', 'name'],
            $this->_db->select('users2', ['id', 'name']
        );
        return $res;
    }

这会返回一个漂亮的数组,其中包含表中的所有值。忽略这些值并跳到下一个合法值的最佳做法是什么?

【问题讨论】:

    标签: php medoo


    【解决方案1】:

    要在 medoo 中使用 WHERE .. NOT LIKE .. 运行查询,您可以将第三个参数传递给包含 where 子句数组的 select()。键是带有运算符的列,值是 - 嗯,值。

    例如,要从没有名称以John 开头的users1 中进行选择,您需要

    $this->_db->select('users1', ['id', 'name'], ['name[!~]' => 'John']);
    

    [!~] 表示NOT LIKE

    【讨论】:

    • 谢谢 Qirel,我可以确认这具有排除某些 calues 的预期效果。
    • ['name[!~]' => 'John'] 等价于 name NOT LIKE "%John%"。如果只想排除以 John 开头的名字,使用 ['name[!~]' => 'John%'] 也更快(相当于 name NOT LIKE "John%")。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-10
    • 2020-01-16
    • 2019-08-23
    • 2011-01-12
    • 1970-01-01
    • 2019-11-22
    相关资源
    最近更新 更多