【问题标题】:SQL SELECT and INSERT statements togetherSQL SELECT 和 INSERT 语句一起使用
【发布时间】:2012-01-11 03:54:05
【问题描述】:

如果我有一个名为 Places 的表,其中包含多个字段和一个主键 placeID,如果将新项目插入表中的最佳方法是自动递增 placeID,但所有其他字段与 placeID= 001 的项目相同。

这就是我正在做的事情

 SELECT* FROM Places WHERE placeID = 001 ... 
 INSERT INTO Places VALUES(...should be values from previous select)

但我不知道该怎么做。

【问题讨论】:

    标签: sql select insert


    【解决方案1】:

    你只是把它倒过来,你需要明确地命名你的列,每一列除了placeIDSELECT列表中:

    INSERT INTO places (col1, col2, col3, col4, every_column_except_placeID)
      SELECT col1, col2, col3, col4, every_column_except_placeID FROM Places WHERE placeID='001'
    

    你应该得到一个带有placeID自动递增的新行。

    【讨论】:

    • 几乎正确,但您需要指定每一列包括 placeID 或指定您将选择的特定列。插入位置(col1、col2、col3、col4、everycolumn_except_palceID)(从位置中选择 col1、col2、col3、col4、everycolumn_except_palceID ...)
    【解决方案2】:

    如果一个 SQL 语句看起来很乱,那么我将其分解并使用更多资源。对于需要压缩每一滴性能的大型网络应用程序来说,这是一个糟糕的解决方案。

    但是,大多数情况下,对于要求不高的情况,一个漂亮的视觉逻辑过程就可以了。

    我会选择我的信息,并使用高级语言的内部循环将结果放入插入语句中。我没有受到足够的惩罚来制作更多的插入物。在许多共享主机上,您可以在一段时间内进行多少次查询是有限制的,因此您可能需要构建更少但更大的插入语句。

    为此,我可能会使用 PHP 来处理我的数据和 SQL,我相信以下内容可以适应您想要使用的任何语言。

    <!php
    $placeid=001
    
    $sql1="SELECT column,othercolumn,maybeasterisk FROM table1 WHERE PlaceID=$placeid";
    $sql2="INSERT into table2 (column,othercolumn,thirdcolumn,whymore,so-on) VALUES ('$data1a','$data1b','$data1c','$data1d','$data1e')";
    $sql3="INSERT into table2 (column,othercolumn,thirdcolumn,whymore,so-on) VALUES $bigstring";
    
    $counter=0;
    
    $result1=mysql_query($sql1);
    while ($resultrow1=mysql_fetch_array($result1)) {
    
    $data1a=$resultrow1['1'];
    $data1b=$resultrow1['2'];
    $data1c=$resultrow1['3'];
    $data1d=$resultrow1['whymore'];
    $data1e=$resultrow1['so-on'];    
    
    if ($abillioninsertsareokay='true') {
    mysql_query($sql2);
    }
    
    else {
    //a billion inserts isn't okay
    //build an array that we can use in a separate loop
    for ($i=0, $i<=5, $i++) {
    $sql3arraydata['$counter']['$i']
    $counter++
    }
    
    }
    
    $counter=0;
    
    if ($abillioninsertsareokay!='true') {
    foreach $sql3arraydata['$counter'] {
    
    if ($counter>0) {
    $bigstring.=', ';
    }
    
    $bigstring.="('$sql3arraydata['$counter'][1]','$sql3arraydata['$counter'][2]','$sql3arraydata['$counter'][3]','$sql3arraydata['$counter'][4]','$sql3arraydata['$counter'][5]')";
    }
    
    mysql_query($sql3); //one huge insert statement
    
    }
    
    ?>
    
    It may be the wrong way to look at things, but sometimes you just have to crank out mediocre code that works to get the job done. Yes, that would be great if you had all the time in the world to make it super awesome, but if this is the kind of question you are asking-- probably not a mission critical system for a mass amount of people.
    

    【讨论】:

      猜你喜欢
      • 2011-03-04
      • 2012-10-18
      • 2010-11-09
      • 2019-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多