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 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding