doufei3561 2016-03-12 17:54
浏览 73
已采纳

只有当另一个字段为空或null时,mysql才会从行中删除

I have a php / mysql event lister and I am coming unstuck where I only want to auto delete a record if it's startdate (field_1) has passed and there is no future enddate set (field_5), so field_5 would need to be empty before it's deleted. Both start and end date fields are set to null on mysql dbase. I can get the code to delete a lapsed event using my code below but I have no idea how to apply mysql logic / conditional code required to check if the enddate (field_5) is set or empty, thereby preventing a deletion if that field is set with a string date. Can anyone advise please ?

$eventstart ="STR_TO_DATE(field_1, '%m/%d/%Y')";
$eventend ="STR_TO_DATE(field_5, '%m/%d/%Y')";
mysql_query("DELETE FROM events3 WHERE $eventstart < $date ") or  die(mysql_error());
  • 写回答

2条回答 默认 最新

  • doujiao7325 2016-03-12 18:03
    关注

    Try to add checking if field_5 IS NULL or equal empty string with this code:

    $eventstart ="STR_TO_DATE(field_1, '%m/%d/%Y')";
    $eventend ="STR_TO_DATE(field_5, '%m/%d/%Y')";
    mysql_query("DELETE FROM events3 
        WHERE $eventstart < $date AND 
        (field_5 IS NULL OR field_5 ='')"
    ) or  die(mysql_error());
    

    If we need to delete rows with a lapsed set of start and end dates (so both field_1 & field_5 were past current date and warranted deletion) we can use this code:

    $eventstart ="STR_TO_DATE(field_1, '%m/%d/%Y')";
    $eventend ="STR_TO_DATE(field_5, '%m/%d/%Y')";
    mysql_query("DELETE FROM events3 
        WHERE $eventstart < $date AND 
        (field_5 IS NULL OR field_5 ='' OR $eventend < $date)"
    ) or  die(mysql_error());
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应