gpt4 book ai didi

mysql - 如何在 'IF' 语句中使用 'SELECT' 并在 mysql 上插入

转载 作者:行者123 更新时间:2023-11-29 17:55:34 25 4
gpt4 key购买 nike

我的 table 是

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

我像这样在 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

运行后,为什么会出现这样的错误

Error Code: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTO tabless (name_stuff) VALUES ('stuff').

以及如何创建正确的选择条件然后插入?谢谢

最佳答案

我认为不可能编写这样的插入语句。你可以这样做 -

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');

关于mysql - 如何在 'IF' 语句中使用 'SELECT' 并在 mysql 上插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48885873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com