我需要撰寫查詢,將新行插入 cms_objects_channels 但僅在 WHERE 子句中的給定類別內,但 VALUES 需要是
id_objects 必須等于來自子查詢的 ID
id_channels 必須是 8(靜態的,所以很容易)
contract_date 必須是從今天起 6 個月
contract_amount 必須是 1000(靜態)
params 有點棘手,因為它必須是“hotel_id=id_objects”,比如“hotel_id=123”
狀態必須是“新的”(靜態)
hotel_id 必須與 id_objects 相同
active 必須設定為 1(靜態)
我寫了這樣的東西,但它不起作用,值只是為了幫助形象化
INSERT INTO hotres_panel.cms_objects_channels ( id_objects
,id_channels
,contract_date
,contract_amount
,params
,status
,hotel_id
,active )
WHERE id_objects IN
( SELECT id
FROM hotres_panel.cms_objects where ( category_id = 175 OR 176 OR 217 OR 180 OR 178 OR 218 OR 196)
AND (active = 1) AND (test = 0)
AND contract_date >= CONCAT(CURDATE()))
VALUES ( '961', '8', '2022-08-20', '199','hotel_id=123' 'new', '961', '1');
uj5u.com熱心網友回復:
INSERT INTO hotres_panel.cms_objects_channels (id_objects, id_channels, contract_date, contract_amount, params, status, hotel_id, active)
SELECT 961, 8, '2022-08-20', 199,'hotel_id=123', 'new', 961, 1
WHERE 961 IN ( SELECT id
FROM hotres_panel.cms_objects
where category_id IN (175, 176, 217, 180, 178, 218, 196)
AND active = 1
AND test = 0
AND contract_date >= CONCAT(CURDATE()) )
;
uj5u.com熱心網友回復:
下面的查詢是給你一個提示,你可以根據自己的需要進行修改。基于:
我寫了這樣的東西,但它不起作用,值只是為了幫助形象化
要構建動態查詢,您需要選擇在插入陳述句中使用的所有列。正如@Akina 在評論中提到的
INSERT .. VALUES 不允許 WHERE 子句,您應該使用 INSERT .. SELECT。
INSERT INTO hotres_panel.cms_objects_channels ( id_objects
,id_channels
,contract_date
,contract_amount
,params
,status
,hotel_id
,active )
SELECT a.id_objects
,a.id_channels
,a.contract_date
,a.contract_amount
,a.params
,a.status
,a.hotel_id
,a.active )
FROM (
SELECT ---all the columns you need with the below condition
FROM
WHERE id_objects IN
( SELECT id
FROM hotres_panel.cms_objects where ( category_id = 175 OR 176 OR 217 OR 180 OR 178 OR 218 OR 196)
AND (active = 1) AND (test = 0)
AND contract_date >= CONCAT(CURDATE()) as a
) ;
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/330919.html
標籤:mysql
上一篇:這是升級標準庫鎖的有效方法嗎?
