MySQL插入表中,如果为空否则更新

时间:2018-07-25 20:14:22

标签: mysql

我想将数据放入来自其他两个表的第三个表中。但是,如果用户已经存在,我只想更新用户点。我已经看过“ ON DUPLICATE KEY UPDATE”,但不知道如何在我的插入中设置它。

<!DOCTYPE html>
<html>
  <head>
   <meta charset="utf-8">
   <title>A2Q4</title>
   <link rel="stylesheet" href="style.css">
  </head>
 <body>
   <script>
    function NumberStats(number) { if(number.indexOf('soen287')>=0||number.indexOf('comp249')>=0||number.indexOf('assignment2')>=0) {
      number = number.replace(/soen287/gi,"SOEN287");
      number = number.replace(/comp249/gi,"COMP249");
      number =number.replace(/assignment2/gi,"ASSIGNMENT2");
      document.getElementById("p1").innerHTML ="&gt;&gt; "+number;
    } else{
      number=number.toUpperCase();
      document.getElementById("p1").innerHTML ="&gt;&gt; "+number;
    }
  }
   </script>
   <center>
   <br>
   <form name="myForm">
     <h1>Text detector<br/>A program created by Mohanad Arafe</h1>
     <input type="text" name="amount" placeholder="Enter your sentence"/>
     <input type="button" onclick="NumberStats(amount.value)" value="Try it">
     <p id="p1"></p>
   </form>
 </body>
</html>

1 个答案:

答案 0 :(得分:0)

我使用了ON DUPLICATE KEY UPDATE points =我插入后的点,什么也没发生。但是添加VALUES(points)可以解决问题。谢谢巴尔玛。

   INSERT INTO toplist( user_id, name, lastname, points )
    SELECT O.user_id, name, lastname, SUM( points ) AS points
    FROM userdata AS C, predictions AS O
    WHERE O.lid =2020
    AND C.user_id = O.user_id 
    GROUP BY O.user_id
    ON DUPLICATE KEY UPDATE points = VALUES(points)