简体   繁体   English

如何在mysql上的插入中在“ SELECT”语句中使用“ IF”

[英]how t use 'IF' in 'SELECT' statement with insert on mysql

my table is 我的桌子是

id_stuff | kd_insert | name_stuff | date_createdd 
1        | 1         | example    | 2018-01-01

i create the query in mysql like this 我这样在mysql中创建查询

SELECT IF ((DATE_FORMAT(NOW(),'%Y-%m') > DATE_FORMAT(MAX(date_createdd),'%Y-%m')), 
(
  INSERT INTO tabless (name_stuff) VALUES ('stuff')
), 
(
 null
)
 ) FROM tabless GROUP BY id_stuff ORDER BY kd_insert

after i run, why i get some error like this 我跑步后为什么会出现这样的错误

Error Code: 1064 You have an error in your SQL syntax; 错误代码:1064您的SQL语法错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTO tabless (name_stuff) VALUES ('stuff'). 查看与您的MySQL服务器版本相对应的手册,以获取在'INTO表(name_stuff)VALUES('stuff')附近使用的正确语法。

and how to create the right select with condition then insert ? 以及如何创建select with condition then insert的正确select with condition then insert thanks 谢谢

I don't think it is possible to write insert statement like that. 我认为不可能编写这样的插入语句。 You can do this - 你可以这样做 -

SELECT
  if(
    (DATE_FORMAT(NOW(), '%Y-%m')) > DATE_FORMAT(MAX(date_createdd), '%Y-%m'),
    (@value = 'stuff'),
    null
  )
FROM
  tabless
GROUP BY
  id_stuff
ORDER BY
  kd_insert;
INSERT INTO tabless (name_stuff) VALUES ('stuff');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM