【问题标题】:using IF statement in mySql INSERT在 mySql INSERT 中使用 IF 语句
【发布时间】:2021-04-29 03:37:22
【问题描述】:

例如如何在 mySql 中使用 if 语句 我有这个代码


INSERT INTO covidtest ( covidTestResult ,CovidTestDate, Idnumber ) VALUES (?,?,? )

这将插入用户的 covidTest 如果用户在 14 天内进行了测试,我想使用 if 语句

它不应该再次插入它

提前致谢

【问题讨论】:

    标签: php mysql reactjs database


    【解决方案1】:

    如果我理解正确,如果您插入的测试日期超过 14 天,您不想插入:

    INSERT INTO covidtest ( covidTestResult ,CovidTestDate, Idnumber ) 
    Select ? ,? ,? 
    where  ? > current_date - interval 14 day
    

    在你再次传递日期的地方

    在上面的查询中,您使用 select 语句插入数据,因此您可以使用 where 来检查您的条件(S)或进行一些完整性检查 ,它也非常适合我,请参阅 dbfiddle here

    在小提琴第一个查询满足条件并插入数据,但第二个查询不满足,它不插入数据。

    【讨论】:

    • 是的,你很好理解,你能解释更多
    • ER_PARSE_ERROR:您的 sql 语法有错误,请检查与您的 mysql 服务器版本相对应的手册,以获取在 '? 附近使用的正确语法。 > current_date - inetrval 14 day' 在第 1 行
    • INSERT INTO covidtest ( covidTestResult ,CovidTestDate, Idnumber ) Select ? ,? ,? from <Table Name> where ? > current_date - interval 14 day
    • @Fabio,这可能是我修复的拼写问题,否则它工作正常,请参阅我的答案中的小提琴链接
    • 非常感谢您的帮助
    猜你喜欢
    • 2011-10-14
    • 2016-06-10
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    • 2011-12-04
    • 1970-01-01
    • 2015-04-30
    • 1970-01-01
    相关资源
    最近更新 更多