English 中文(简体)
是否有更好的办法来管理这一神话的更新?
原标题:Is there a better way to run this mysql update?
  • 时间:2012-04-15 15:13:27
  •  标签:
  • mysql
  • insert

我有以下代码,首先从数据库中检索每一种独特的风格,然后通过产品表格进行检索,以找到与每种风格相关的术语,并以风格和产品更新风格表格:

$query = "Select id,style,terms from su_styles";

if ($results = $sudb->query($query)) {

while($result = $results->fetch_array(MYSQLI_ASSOC)) {

      $id=$result[ id ];
      $style=$result[ style ];
      $terms=$result[ terms ];      

$query= "INSERT IGNORE INTO su_stylerefs (mykey,id) 
    SELECT mykey,$id FROM su_pref where (match(name) against ( $terms  in Boolean Mode)) 
    ON DUPLICATE KEY UPDATE id=$id, mykey = su_pref.mykey";     

$sudb->query($query);

难道我可以把这个问题变成一个问题,而不是通过第一个问题的结果 lo? 我有另外10个类似问题,需要每天处理,在第一个查询中,有些表格可能有100个记录,这意味着与数据库有数百个链接,需要一手。

事先感谢你

问题回答

我首先想起所有这些行文,然后做更新:

INSERT INTO su_stylerefs (mykey, id)
VALUES ($mykey[0], $id[0]), ($mykey[1] $id[1]), ...
ON DUPLICATE KEY UPDATE id=$id, mykey = su_pref.mykey

<>2nd Solutions:

INSERT IGNORE INTO su_stylerefs (mykey,id) 
SELECT sp.mykey, ss.id
FROM su_styles AS ss
INNER JOIN su_pref AS sp
ON match(sp.name) against (ss.terms in Boolean Mode)
ON DUPLICATE KEY UPDATE id = VALUES(id), mykey = VALUES(mykey)

这可能是一个更好的解决办法。





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

热门标签