简体   繁体   English

将表中的许多行插入另一表中的唯一行

[英]Insert many rows from a table into one unique row in another table

I have a table named order where there is set a column name list 我有一个名为order的表order其中设置了一个列名list

TABLE list
id |  list      | price    | date
---------------------------------------
1  | Cigar      |  5.00    | 2016-06-30
2  | Beer       |  6.00    | 2016-06-30
3  | Whiskey    |  20.00   | 2016-06-30
4  | Bacon      |  10.00   | 2016-06-30

I'd like to insert the list into another table named confirmation in a way that all of them could be in a same row! 我想将list插入到名为confirmation另一张表中,以使它们都可以在同一行中! However, it doesn't work and is inserting in many rows! 但是,它不起作用,并且插入了很多行!

The way I want 我想要的方式

TABLE confirmation
id |            theorder             
--------------------------------
1  | Cigar, Beer, Whiskey, Bacon

The way is showing 方式显示

TABLE confirmation
id |  theorder      
--------------
1  | Cigar,
2  | Beer,
3  | Whiskey,
4  | Bacon,

Here is the code: I'm working with foreach! 代码如下:我正在使用foreach!

$sql     = "SELECT list FROM order";
$result  = $conn->query($sql);
$getList = $result->fetchAll(PDO::FETCH_ASSOC);

foreach($getOrder as $order) {
  $products = $order['theorder'] . ', ';
  $sql      = "INSERT INTO confirmation (theorder) VALUES ('$products')";
  $result   = $conn->query($sql);
}

Every time you perform an INSERT query it creates a new row. 每次执行INSERT查询时,都会创建一个新行。 Since you're doing this in a loop, it creates a new row for each product. 由于您要循环执行此操作,因此会为每个产品创建一个新行。

If you want to combine items, use GROUP_CONCAT : 如果要合并项目,请使用GROUP_CONCAT

INSERT INTO confirmation (theorder)
SELECT GROUP_CONCAT(list SEPARATOR ', ')
FROM `order`

Notice that you need to quote the table name order with backticks because it's a reserved word. 注意,您需要用反引号引起表名order引用,因为这是保留字。 It's generally best to avoid using reserved words as table and column names, since if you forget to quote it you'll get a confusing syntax error. 通常最好避免使用保留字作为表名和列名,因为如果忘记引用,则会出现令人困惑的语法错误。 See Syntax error due to using a reserved word as a table or column name in MySQL 请参见由于在MySQL中使用保留字作为表或列名而导致的语法错误

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

相关问题 MYSQL单一查询可从一个表中检索单行,也可从另一表中检索多个行作为单个字段 - MYSQL Single query to retrieve both single row from one table and many rows as a single field from another 从一个表中删除ROWS,然后在MYSQLI中插入到另一个表中 - Delete ROWS from one table and then INSERT into another TABLE in MYSQLI 如何将表的行从一个数据库插入到另一个数据库(使用两个PDO) - How to insert rows of a table from one database to another (with two PDO) 将一张表中的所有行插入到另一张表中+末尾添加时间戳 - Insert all rows from one table to another + time stamp at the end 查询以获取表中的1行以及该表中引用的另一张表中的许多行 - Query to fetch 1 row in table, and many rows from another table, referenced in that row MySQL将数据从一个表插入到另一个选定的行 - Mysql insert data from one table to another selected rows 将三行与另一张表中的一行连接起来 - Join three rows with one row from another table 从一张桌子插入到另一张桌子 - Insert from one table to another PHP MySQL使用where子句INSERT来自另一个表的数据,并将唯一值插入到相同的行中 - PHP MySQL INSERT data from another table using the where clause and insert unique values to the same rows 从另一个表中按唯一值分组的一个表中选择所有行 - Selecting all rows from one table grouped by unique values from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM