Php 无法获取sql中按组行筛选的行
我有一个表,其中有一个开始日期和结束日期。这些行是按唯一id分组的,例如,行id 4,5有组id 5700045,行id 1,2,3有组id 5700044。我必须只选择那些组中所有行的开始列都大于当前日期的组。例如,如果当前日期为15,则5700045不应显示在结果中Php 无法获取sql中按组行筛选的行,php,mysql,codeigniter,codeigniter-3,Php,Mysql,Codeigniter,Codeigniter 3,我有一个表,其中有一个开始日期和结束日期。这些行是按唯一id分组的,例如,行id 4,5有组id 5700045,行id 1,2,3有组id 5700044。我必须只选择那些组中所有行的开始列都大于当前日期的组。例如,如果当前日期为15,则5700045不应显示在结果中 id start end group_identify_id 1 2019-07-18 07:15:00 2019-0
id start end group_identify_id
1 2019-07-18 07:15:00 2019-07-18 08:15:00 5700044
2 2019-07-16 07:15:00 2019-07-16 08:15:00 5700044
3 2019-07-17 07:15:00 2019-07-17 08:15:00 5700044
4 2019-07-15 07:15:00 2019-07-15 08:15:00 5700045
5 2019-07-14 07:15:00 2019-07-14 08:15:00 5700045
结果应该是这样的:
$results = $this->db->where('where('DATE(start) >=',$currentDate)->get('schedule_diary')->result();
结果:
我试着这样做:
$results = $this->db->where('where('DATE(start) >=',$currentDate)->get('schedule_diary')->result();
但它匹配的是整行,而不是一组行。下面是原始查询,它将为您提供所需的结果
SELECT * FROM schedule_diary WHERE group_identify_id NON IN ( SELECT group_identify_id FROM schedule_diary WHERE start <= $currentDate) );
您需要将此查询转换为CodeIgniter框架,或者您也可以使用$this->db->query'RawQueryHere'我不确定,但我认为,这里有点奇怪:
$results = $this->db->where('where('DATE(start) >=',$currentDate)->get('schedule_diary')->result();
尝试在查询中删除它:
$results = $this->db->where('DATE(start) >=',$currentDate)->get('schedule_diary')->result();
更新
如果组\u identific\u id中的值为null,则可以使用类似的方法进行测试:
我认为这是适合你的代码 使用此原始查询或将其转换为CodeIgniter格式
SELECT * FROM schedule_diary WHERE DATE(START) > "$currentDate" AND group_identify_id NOT IN (SELECT distinct group_identify_id FROM test_1 WHERE DATE(START) <= "$currentDate")
它将完美地工作
工作截图
SELECT * FROM schedule_diary WHERE DATE(START) > date("2019-07-14") AND group_identify_id NOT IN (SELECT distinct group_identify_id FROM schedule_diary WHERE DATE(START) <= date("2019-07-14"))
请尝试以下查询:
$this->db->where('start >', $currentDate)->get('schedule_diary')->result();
您可以尝试这个原始查询
select * from schedule_diary group by group_identify_id having start > CURDATE()
对于codeigniter,您可以使用
$this->db->table('schedule_diary')->group_by('group_identify_id')->having('start', $currentDate);
$currentDate的值是多少?这个变量包含我感兴趣的todays Date,$currentDate的格式是什么。。是否具有可比性?日期2019-07-15 12:14的格式:50@user3653474尝试从$CurrentDate中删除时间,因为它没有返回任何行,我的数据库中的组\u identific\u id中有一些值为null。确定。现在使用不存在进行测试。我尝试了此查询,但如果删除where条件,则不会返回任何结果,然后它将结果更改CASTt1.start打印为DATE,而不是DATEt1.start。请注意,GETDATE函数是今天的。你今天有安排吗?我正在我的计算机上进行测试,它正在SQL server中工作。或者直接使用以下查询进行测试:``选择*从不存在的日程安排日志中选择*从日程安排日志中选择*作为t1,其中t1.group\U Identification\U id=Test.group\U Identification\U id和CASTt1.start as DATE@谢谢我尝试了此查询,但未返回任何结果:$result=$this->db->query'SELECT*FROM table_name WHERE DATESTART>$currentDate AND group_unique_key不在从table_name WHERE DATESTART@user3653474中选择不同的group_unique_key,传递日期的格式。。使用Y-m-d格式或尝试此查询。。从计划中选择*从DATESTART>date$currentDate和group_Identification_id不在其中从测试中选择不同的group_Identification_id,其中DATESTART@user3653474编辑了我的问题尝试运行该查询。。不要为测试传递日期参数。谢谢,它正在工作。如果我必须使用一个条件,如开始时间和结束时间,我还想澄清一件事。每次我都必须在子查询中使用主查询的相反条件。例如,时间“$start\u time.”和时间“$end\u time.”之间的和TIMEstart。因此,在子查询中,我们必须使用NOT BETH。BETH是>=和$result=$this->db->query“SELECT*FROM table\u name WHERE DATESTART>$currentDate和group\u unique\u key不在DATESTABLE\u name WHERE DATESTABLE\u name WHERE DATESTABLE>中选择不同的GROU
$this->db->table('schedule_diary')->group_by('group_identify_id')->having('start', $currentDate);