English 中文(简体)
a. 采用条款更新表格的属性
原标题:writing a query for updating a table using another table s attributes by using a clause
  • 时间:2012-04-25 21:36:54
  •  标签:
  • mysql

I WANT TO DOSOMETHING LIKE THIS:

CREATE TRIGGER addwinner AFTER INSERT ON bids
FOR EACH ROW BEGIN 
IF exists(select * from wins as LT where LT.item_index=NEW.item_index) THEN

SELECT item_index, MIN( bid_amount ) 
FROM update_winnerslist AS a1
WHERE a1.item_index =  NEW.item_index;
UPDATE wins SET email_id=NEW.emai_id, bid_amount=a1.bid_amount where wins.item_index=a1.item_index;

END IF;
END;

基本上,我想通过比较某些属性来更新一个表格。

最佳回答

如以下解释:,在手册中,在确定使用<代码的程序/触发/etc时;,在本机构内单独指挥,你需要界定替代物,以便 CREATE 指挥被解读为单一声明。

<>UPDATE with the results of SlectT, 如您所述,你可以做以下任何工作:

  1. Save the results of the SlectT in a/2007/5 which they subsequently used in the UPDATE:

    DELIMITER ;; -- or anything else you like
    
    CREATE TRIGGER addwinner AFTER INSERT ON bids FOR EACH ROW
      IF EXISTS (SELECT * FROM wins AS LT WHERE LT.item_index=NEW.item_index)
      THEN
        SELECT MIN(bid_amount) INTO @bid_amount
        FROM   update_winnerslist AS a1
        WHERE  a1.item_index = NEW.item_index;
    
        UPDATE wins
        SET    email_id=NEW.emai_id, bid_amount=@bid_amount
        WHERE  wins.item_index=a1.item_index;
      END IF;;  -- whatever command delimiter you chose above goes here
    
    DELIMITER ; -- reset to normal
    
  2. 仅使用一个子序列(只要它没有提及你重新更新的表格):

    DELIMITER ;; -- or anything else you like
    
    CREATE TRIGGER addwinner AFTER INSERT ON bids FOR EACH ROW
      IF EXISTS (SELECT * FROM wins AS LT WHERE LT.item_index=NEW.item_index)
      THEN
        UPDATE wins
        SET    wins.email_id=NEW.emai_id, wins.bid_amount=(
          SELECT MIN(bid_amount)
          FROM update_winnerslist AS a1
          WHERE a1.item_index = NEW.item_index
        )
        WHERE  item_index = NEW.item_index
      END IF;;  -- whatever command delimiter you chose above goes here
    
    DELIMITER ; -- reset to normal
    
  3. 或只是加入表格:

    DELIMITER ;; -- or anything else you like
    
    CREATE TRIGGER addwinner AFTER INSERT ON bids FOR EACH ROW
      IF EXISTS (SELECT * FROM wins AS LT WHERE LT.item_index=NEW.item_index)
      THEN
        UPDATE wins JOIN update_winnerslist AS a1 USING (item_index)
        SET    wins.email_id=NEW.emai_id, wins.bid_amount=MIN(a1.bid_amount);
        WHERE  item_index = NEW.item_index
      END IF;;  -- whatever command delimiter you chose above goes here
    
    DELIMITER ; -- reset to normal
    
问题回答

暂无回答




相关问题
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 ...

热门标签