簡體   English   中英

如果選擇的值> x,則插入MySQL

[英]MySQL Insert if selected value > x

我正在嘗試編寫僅基於選擇查詢結果進行插入的MySQL插入。 我發現了一些在查詢中使用if-then-endIf邏輯的信息,但是沒有運氣。 那么我想要完成的一般想法是:

if
 result of select is > 0
 perform insert and select true
 else select false
endIf

我想以這種方式運行它,因為這似乎是阻止插入超出預定義限制的最簡單方法。

無論如何,如果有人能指出正確的方向來構造/嵌套語句,我將不勝感激。

謝謝。

編輯

萬一有人想知道,這就是我最后得到的結果:

insert into users_to_classes select ? , ?, 4 from (select class_id, count(*) as students from users_to_classes where participation_level=4 group by class_id) as numUtC, classes where numUtC.class_id=classes.class_id and classes.class_id=? and classes.spaces - ifnull(numUtC.students,0) > 0

用作mysqli准備語句的地方。

再次感謝您的幫助

您可以嘗試進行一些相關更改:

INSERT INTO <destination_table>
   SELECT <values>
   FROM <source_table>
   WHERE <value> > x

進行輕微編輯以使其更清晰。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM