简体   繁体   English

多SQL选择然后插入

[英]multi sql select then insert

i have a total of 6 different tables that i select from then insert to a table before i do another select i was wondering if there was a way to skip the insert part and just do a select and combine all table data. 我总共有6个不同的表可供选择,然后再插入一个表,然后再执行另一个选择,我想知道是否有一种方法可以跳过插入部分,而只是进行选择并合并所有表数据。 the problem with the select and insert then select aproach is it gets really slow when there are about 1k+ records inserted. 选择并插入然后选择方法的问题是,当插入大约1k +条记录时,它变得非常慢。 it takes about 30sec to 1min or more 大约需要30秒到1分钟或更长时间

im trying something like this 我正在尝试这样的事情

$sql = "select 1";
$statement = $conn->query($sql);
$rowset = $statement->fetchAll();

$sql1 = "select 2";
$statement1 = $conn->query($sql1);
$rowset1 = $statement1->fetchAll();

$combine = array_merge($rowset,$rowset1);


foreach ($combine as $key => $part) {
       $sort[$key] = strtotime($part['date']);
}
array_multisort($sort, SORT_DESC, $combine);

To me it seems that you are replicating in php what you could do in sql. 对我来说,您似乎在php中复制了可以在sql中进行的操作。 The above code in sql looks sg like this: 上面的sql代码在sg中看起来像这样:

(select 1)
union all
(select 2)
order by date desc

You may have to tweak the order by clause depending on what data you exactly have in the date field. 您可能必须根据日期字段中确切包含的数据来调整order by子句。 Otherwise, the above sql code should produce the exactly same results as your php code. 否则,上述sql代码应产生与php代码完全相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM