Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.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/9/loops/2.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
Mysql 检查一组日期是否小于现在()_Mysql_Loops_Stored Functions - Fatal编程技术网

Mysql 检查一组日期是否小于现在()

Mysql 检查一组日期是否小于现在(),mysql,loops,stored-functions,Mysql,Loops,Stored Functions,我有一个“事件”表和一个“事件日期”表。 “事件日期”在“事件”表中引用了事件id。 我想构建一个存储函数来检查NOW()是否大于引用特定事件id的“events\u dates”中的所有日期。 我编写该函数是为了得到一个tinyint(1)值(如果为真,则为1,如果为假,则为0),但我总是得到false。 此外,我认为我在使用循环时迷失了方向。 你会在我的位置上做什么 我想在更大的查询中使用该函数,并像这样使用它: SELECT e.*, ed.* FROM events e INNER JO

我有一个“事件”表和一个“事件日期”表。 “事件日期”在“事件”表中引用了事件id。 我想构建一个存储函数来检查NOW()是否大于引用特定事件id的“events\u dates”中的所有日期。 我编写该函数是为了得到一个tinyint(1)值(如果为真,则为1,如果为假,则为0),但我总是得到false。 此外,我认为我在使用循环时迷失了方向。 你会在我的位置上做什么

我想在更大的查询中使用该函数,并像这样使用它:

SELECT e.*, ed.*
FROM events e INNER JOIN
     events_dates ed
     ON e.event_id = ed.fk_event_id
 WHERE IF (checkIfAllDatesArePassed(e.event_id),DATEDIFF(ed.event_date,NOW())<0,DATEDIFF(ed.event_date,NOW())>0)
选择e.*,ed*
从事件e内部连接
活动日期
在e.event_id=ed.fk_event_id上
其中IF(checkIfAllDatesArePassed(e.event_id),DATEDIFF(ed.event_date,NOW())0)
事实上,这有点复杂,但我相信你明白了重点:)

谢谢大家。

如果您只想让符合此标准的事件与标志一起出现:

SELECT ed.event_id,
       (max(event_date) < now()) as AllDatesPast
FROM events_dates ed
group by event_id;
如果你想知道日期:

SELECT e.*, ed.*
FROM events e join
     events_dates ed
     on e.event_id = ed.fk_event_id
WHERE not exists (select 1
                  from event_dates ed2
                  where e.event_id = ed2.event_id and
                        event_date > now()
                 ) ;

我会使用ALL关键字,例如

select *
from events
where now() > all (select event_date from events_dates where fk_event_id = event_id)
假设event_id是函数的一个参数

然而,我无法理解所通过的支票日期应该是什么

根据评论更新:

drop function if exists checkIfAllDatesArePassed;
delimiter $$
create function checkIfAllDatesArePassed(event_id integer)
returns integer
language sql
begin
return select case
when now() > all (select event_date from events_dates where fk_event_id = event_id) then 1
else 0;
end;
$$
delimiter;

可以吗?

CheckIfallDatesRepassed是我正在尝试构建的函数:)如果所有日期都小于现在,则该函数必须返回1(),如果至少有一个日期大于现在(),则该函数必须返回0。在所有关键字都存在之后,我们可以尝试使用all关键字:)我已更新提交内容。但是,请检查该函数的语法是否正确。我已经有一段时间没有在MySQL中编写函数了。这正是我想要的:)谢谢你的解决方案。我只需要函数返回1或0。请查看我对@codeblur solution的评论。谢谢,请提供样本数据和预期结果。
drop function if exists checkIfAllDatesArePassed;
delimiter $$
create function checkIfAllDatesArePassed(event_id integer)
returns integer
language sql
begin
return select case
when now() > all (select event_date from events_dates where fk_event_id = event_id) then 1
else 0;
end;
$$
delimiter;