Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.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 如何获得固定客户名单_Php_Mysql_Sql - Fatal编程技术网

Php 如何获得固定客户名单

Php 如何获得固定客户名单,php,mysql,sql,Php,Mysql,Sql,我想获得常规客户记录的列表。有人能帮我在mysql查询中获取常规客户记录吗 常规客户记录定义:在过去3个月内每月下单订单的客户称为常规客户 例如:客户A在6月、7月和8月下单,称为常规订单,但如果他在5月、7月和8月下单,则不是常规客户 以下是我的表格清单: CREATE TABLE IF NOT EXISTS `customer_mst` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `firstName` varchar(50) NOT NULL,

我想获得常规客户记录的列表。有人能帮我在mysql查询中获取常规客户记录吗

常规客户记录定义:在过去3个月内每月下单订单的客户称为常规客户

例如:客户A在6月、7月和8月下单,称为常规订单,但如果他在5月、7月和8月下单,则不是常规客户

以下是我的表格清单:

CREATE TABLE IF NOT EXISTS `customer_mst` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `firstName` varchar(50) NOT NULL,
  `lastName` varchar(50) NOT NULL,
  `email` varchar(150) NOT NULL,
  `phone` varchar(20) NOT NULL,
  `dateCreated` datetime NOT NULL,
  `lastActivity` datetime NOT NULL,
  `ipAddress` varchar(40) NOT NULL,
  PRIMARY KEY (`ID`)
)

CREATE TABLE IF NOT EXISTS `order_mst` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `custId` int(11) NOT NULL COMMENT 'FK customer_mst(ID)',
  `grandTotal` float NOT NULL,
  `createdDate` datetime NOT NULL,
  PRIMARY KEY (`ID`),
  KEY `custId` (`custId`)
) 

这是最简单的方法,但不是最快的:

select * from customer_mst
where ID in (
    select custId from order_mst
    where createdDate <= (NOW() - INTERVAL 2 MONTH) and createdDate > (NOW() - INTERVAL 3 MONTH)
)
and ID in (
    select custId from order_mst
    where createdDate <= (NOW() - INTERVAL 1 MONTH) and createdDate > (NOW() - INTERVAL 2 MONTH)
)
and ID in (
    select custId from order_mst
    where createdDate <= NOW() and createdDate > (NOW() - INTERVAL 1 MONTH)
)
提示:尝试使用InnoDB引擎,而不是注释。

试试这个

select customer_mst.*,DATEDIFF(now()-order_mst.createdDate) as diff_days 
from customer_mst
left join order_mst on (order_mst.custId = customer_mst.ID)
group by customer_mst.ID having diff_days  < 90
但效率不高

$month1=mysqli_query(select * from customer_mst
where ID in (
    select custId from order_mst
    where createdDate <= (NOW() - INTERVAL 2 MONTH) and createdDate > (NOW() - INTERVAL 3 MONTH)
));
$rows=mysqli_num_rows($month1);
if($rows>0){
$month2=mysqli_query(select * from customer_mst
where ID in (
    select custId from order_mst
    where createdDate <= (NOW() - INTERVAL 1 MONTH) and createdDate > (NOW() - INTERVAL 2 MONTH)
));
$rows1=mysqli_num_rows($month2);
if($rows1>0){
$month3=mysqli_query(select * from customer_mst
where ID in (
    select custId from order_mst
    where createdDate <= NOW() and createdDate > (NOW() - INTERVAL 1 MONTH)
));
$rows2=mysqli_num_rows($month3);
if($month3>0){
echo 'regular customer';}
};
};

您是否在寻找在过去三个月或连续三个月内下单的客户?这些问题暗示了前者,但该示例适用于后者。此外,客户是否可以在同一个月内下多个订单?@Mureinik 1过去三个月的连续时间2客户在一个月内下任意数量的订单在您的查询中,没有检查连续3个月的订单。如果您想要客户的订单总数,只需添加以下条件:countorder\u mst.ID>=2或者您可以按订单数量排序:order by countorder\u mst.ID,但请注意使用计数不是一个好的做法。相反,你应该在单独的列中保留该计数。这是不正确的解决方案,因为一个月的计数也将超过3,并且它不是连续的。好的,一开始看你的问题时没有得到。为了做到这一点,你必须把所有三个月都放在不同的条件下,并采取相应的行动。我将尝试更新答案。@MaciejSz我认为以下更改将使查询更加完美,您同意吗?在查询中选择*从客户处,其中ID在从订单处选择客户处,其中monthcreatedDate monthNOW-间隔3个月,ID在从订单处选择客户处,其中monthcreatedDate monthNOW-间隔2个月,ID在从订单处选择客户处,其中monthcreatedDate monthNOW-间隔1个月,没有检查连续3个月的订单是的您的权利@Upendra Chaudhari我错过了那部分抱歉。它会给我3个月前下订单的客户记录,但我想要上3个月每月下订单的客户记录检查我的客户:A在6月下单,7月和8月的订单称为常规订单,但如果他在5月、7月和8月下单,那么这不是常规客户。在上述情况下,客户A必须在每个月的6月、7月和8月下单,才能成为正式客户。