Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 基于集合条件的Laravel雄辩查询多条记录_Php_Mysql_Laravel_Eloquent - Fatal编程技术网

Php 基于集合条件的Laravel雄辩查询多条记录

Php 基于集合条件的Laravel雄辩查询多条记录,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,我有一个表单,我的用户可以在其中设置条件来搜索他们的客户数据库,但是我很难将查询整合到一起(在Laravel 5.7中工作) 目前客户可以设置的标准如下: 与X次访问相比,客户的访问次数“正好”多“少” 客户第一次访问比X天“准确地多|少” 客户最后一次访问的时间比X天“准确地多|少” 客户提供商是“facebook | twitter |电子邮件| any” 我现在正试图弄清楚如何将它构建成一个查询,我甚至不能给出一个具体的例子!我的障碍似乎是检查第一个记录&最后一个记录,以确保它符合标准

我有一个表单,我的用户可以在其中设置条件来搜索他们的客户数据库,但是我很难将查询整合到一起(在Laravel 5.7中工作)

目前客户可以设置的标准如下:

  • 与X次访问相比,客户的访问次数“正好”多“少”
  • 客户第一次访问比X天“准确地多|少”
  • 客户最后一次访问的时间比X天“准确地多|少”
  • 客户提供商是“facebook | twitter |电子邮件| any”
我现在正试图弄清楚如何将它构建成一个查询,我甚至不能给出一个具体的例子!我的障碍似乎是检查第一个记录&最后一个记录,以确保它符合标准

SQLFiddle:

我的桌子:

|  id  | name           | email                                       | provider    | created_at
---------------------------------------------------------------------------------------
 | 1 | Mr Smith        | mr.smith@example.com         | facebook | 2018-11-01 09:00:00 | 
 | 2 | Mrs Smith      | mrs.smith@example.com      | facebook  | 2018-11-01 09:00:00 | 
 | 3 | Miss Smith     | miss.smith@example.com    | email        | 2018-11-01 09:00:00 | 
 | 4 | Doctor Smith | doctor.smith@example.com | email        | 2018-11-01 09:00:00 | 
 | 5 | Lord Smith    | lord.smith@example.com      | twitter      | 2018-11-01 09:00:00 | 
 | 6 | Lady Smith    | lady.smith@example.com      | email        | 2018-11-01 09:00:00 | 
 | 7 | Mr Smith        | mr.smith@example.com         | facebook | 2018-11-02 09:00:00 | 
 | 8 | Mrs Smith      | mrs.smith@example.com       | facebook  | 2018-11-02 09:00:00 | 
 | 9 | Doctor Smith | doctor.smith@example.com  | email        | 2018-11-02 09:00:00 | 
 | 10 | Lord Smith   | lord.smith@example.com       | twitter      | 2018-11-02 09:00:00 | 
 | 11 | Lady Smith   | lady.smith@example.com      | email        | 2018-11-02 09:00:00 | 
 | 12 | Mr Smith      | mr.smith@example.com         | facebook | 2018-11-03 09:00:00 | 
 | 13 | Mrs Smith    | mrs.smith@example.com       | facebook | 2018-11-03 09:00:00 | 
 | 14 | Miss Smith   | miss.smith@example.com     | email        | 2018-11-03 09:00:00 | 
 | 15 | Lord Smith   | ord.smith@example.com       | twitter      | 2018-11-03 09:00:00 | 
 | 16 | Lady Smith  | lady.smith@example.com      | email        | 2018-11-03 09:00:00 | 
查询的客户条件示例:

  • 访问次数超过2次的客户
  • 客户第一次访问是在两天前
  • 客户上次访问是在1天前
  • 客户提供商是Facebook
当前查询:

    $Customers = Customer::groupBy('email')
                 ->havingRaw('COUNT(*) > 2’)
                 ->where('created_at', '<', Carbon::now()->subDays(2)->toDateTimeString())
                 ->get();

您要查找的查询是:

select * from customers group by email having count(*) > 2 and min(created_at) <= '2018-10-02 09:00:00' and max(created_at) <= '2018-10-03 09:00:00' and provider = 'facebook'

您要查找的查询是:

select * from customers group by email having count(*) > 2 and min(created_at) <= '2018-10-02 09:00:00' and max(created_at) <= '2018-10-03 09:00:00' and provider = 'facebook'

用MySQL方言进行选择的SQL查询如下

SELECT id, name, email, provider, created_at
FROM customers 
WHERE provider = 'facebook'
GROUP BY email
HAVING 
  count(*) > 2
  AND min(created_at) < date_sub(now(), interval 2 day)
  AND max(created_at) < date_sub(now(), interval 1 day)
选择id、姓名、电子邮件、提供商、创建地址
来自客户
其中provider='facebook'
通过电子邮件分组
有
计数(*)>2
和最小值(创建时间)
这可以翻译成这样一个雄辩的问题

$havingClause = 'count(*) > ? AND min(created_at) < ? AND max(created_at) < ?';
$havingClauseBindings = [
   2,
   Carbon::now()->subDays(2)->toDateTimeString(),
   Carbon::now()->subDays(1)->toDateTimeString()
];
$customers = Customer::where('provider', 'facebook')
                       ->groupBy('email')
                       ->havingRaw($havingClause, $havingClauseBindings)
                       ->get();
$havingClause='count(*)>?和最小值(创建时间)<?和max(创建于)<?';
$havingClauseBindings=[
2.
Carbon::now()->subDays(2)->toDateTimeString(),
Carbon::now()->subDays(1)->toDateTimeString()
];
$customers=Customer::where('provider','facebook')
->groupBy(“电子邮件”)
->HavingLaw($havingClause,$havingClauseBindings)
->get();

用MySQL方言进行选择的SQL查询如下

SELECT id, name, email, provider, created_at
FROM customers 
WHERE provider = 'facebook'
GROUP BY email
HAVING 
  count(*) > 2
  AND min(created_at) < date_sub(now(), interval 2 day)
  AND max(created_at) < date_sub(now(), interval 1 day)
选择id、姓名、电子邮件、提供商、创建地址
来自客户
其中provider='facebook'
通过电子邮件分组
有
计数(*)>2
和最小值(创建时间)
这可以翻译成这样一个雄辩的问题

$havingClause = 'count(*) > ? AND min(created_at) < ? AND max(created_at) < ?';
$havingClauseBindings = [
   2,
   Carbon::now()->subDays(2)->toDateTimeString(),
   Carbon::now()->subDays(1)->toDateTimeString()
];
$customers = Customer::where('provider', 'facebook')
                       ->groupBy('email')
                       ->havingRaw($havingClause, $havingClauseBindings)
                       ->get();
$havingClause='count(*)>?和最小值(创建时间)<?和max(创建于)<?';
$havingClauseBindings=[
2.
Carbon::now()->subDays(2)->toDateTimeString(),
Carbon::now()->subDays(1)->toDateTimeString()
];
$customers=Customer::where('provider','facebook')
->groupBy(“电子邮件”)
->HavingLaw($havingClause,$havingClauseBindings)
->get();

啊,最小和最大,现在我觉得自己很愚蠢!接受你的回答,因为你添加了有说服力的内容,这很有帮助。啊,Min&Max,现在我觉得自己很愚蠢!当你添加了有说服力的、有帮助的内容时,接受作为答案。