English 中文(简体)
MYSQL INSERT FI SUMs >CONTANT
原标题:MYSQL INSERT IF SUMs > CONSTANT

如果2个表格中的3个用户栏数超过一个固定栏,则试图添加一个记录。

I ve searched all over, found You can t putuser factors in IFs,WHEs et s. 缔造者可使用<条码>。 www.un.org/Depts/DGACM/index_french.htm 总损失为一米。 在试图使用<代码>SUM之前,我前面的坏代码就是一个例子。 WHEREs, 条件是:

SELECT SUM(num1) INTO @mun1 FROM table1 WHERE user =  0 ;

SELECT SUM(num2) INTO @mun2 FROM table1 WHERE user =  0 ;

SELECT SUM(num3) INTO @mun3 FROM table2 WHERE column1 =  d  AND user =  0 ;

SET @mun4 = @mun1 - @mun2 - @mun3;

INSERT INTO table2 (user, column1, column2) VALUES ( 0 ,  d ,  100 ) WHERE @mun4 >= 100;
最佳回答

引证:

INSERT INTO table2 (user, column1, column2) 
select  0 ,  d ,  100 
from dual
where (SELECT SUM(num1 + num2) FROM table1 WHERE user =  0 ) +
      (SELECT SUM(num3) FROM table2 WHERE column1 =  d  AND user =  0 ) > 100;

这是“如果条件”问题的一般解决办法:

insert into ... select ... where condition

只有在条件确实的情况下,而且重要的是,如果情况不实,就不会再行了——也就是说,只有在情况真实,否则就没有发生。

问题回答

这与@Bohemian的答复相同,但请添加一个<代码>。 缩略语 停止插入多个记录的条款,因为选定条款可退回多个记录

INSERT INTO table2 (user, column1, column2) 
SELECT       0 ,  d ,  100 
   FROM dual
   WHERE 
         (SELECT SUM(num1 - num2) FROM table1 WHERE user =  0 )
         (SELECT SUM(num3) FROM table2 WHERE column1 =  d  AND user =  0 ) > 
         100 
   LIMIT 1




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签