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

Caio Ferrari

I have a table named order where there is set a column name 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! 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!

$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);
}
Barmar

Every time you perform an INSERT query it creates a new row. 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:

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. 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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to insert one row of a table into two rows of another table

From Dev

Insert new rows into table but copy data from another row in the table

From Dev

selecting unique rows from one table according to another table and then sorting it

From Dev

MYSQL Single query to retrieve both single row from one table and many rows as a single field from another

From Dev

mysql insert unique values from one column to a column of another table

From Dev

SQL One to many join - do not return rows from the 'one' table if ANY row in the 'many' table equals X

From Dev

select multiple column from one table and insert into another as rows

From Dev

Insert multiple rows with single a query from one table into another in Oracle

From Dev

Insert Data from one table to another leaving the already existing rows

From Dev

Insert specific rows from one table in database into another with different columns

From Dev

Join three rows with one row from another table

From Dev

Selecting two rows from another table using one row

From Dev

Subtract rows of one table from another table

From Dev

Select unique pairs of rows from one table that are associated to the same value in another table in SQL

From Dev

Fetching unique rows from one table that columns reference has not in another table

From Dev

PostgreSQL for each row from one table join all rows from another table

From Dev

Insert multiple rows from select into another table

From Dev

insert multiple rows mysql from another table

From Dev

How to insert specific columns and rows of data to an existing table from one table to another?

From Dev

Insert multiple rows in one table based on number in another table

From Dev

How to insert multiple rows into one table for each id of another table

From Dev

Create trigger to insert primary key from one table upon creation of new row, into column of another table in MySQL

From Dev

Foreach id in one table, insert row in another table

From Dev

Link one row of table to multiple rows in another table in MySQL

From Dev

MySQL query - single row in one table with multiple rows in another table

From Dev

SELECT one row of a table, And all rows in another table

From Dev

Insert rows from one table to another but only those rows that have no duplicates

From Java

SQL query INSERT SELECT COUNT from one table to another row by row

From Dev

Update and insert to one table from another

Related Related

  1. 1

    How to insert one row of a table into two rows of another table

  2. 2

    Insert new rows into table but copy data from another row in the table

  3. 3

    selecting unique rows from one table according to another table and then sorting it

  4. 4

    MYSQL Single query to retrieve both single row from one table and many rows as a single field from another

  5. 5

    mysql insert unique values from one column to a column of another table

  6. 6

    SQL One to many join - do not return rows from the 'one' table if ANY row in the 'many' table equals X

  7. 7

    select multiple column from one table and insert into another as rows

  8. 8

    Insert multiple rows with single a query from one table into another in Oracle

  9. 9

    Insert Data from one table to another leaving the already existing rows

  10. 10

    Insert specific rows from one table in database into another with different columns

  11. 11

    Join three rows with one row from another table

  12. 12

    Selecting two rows from another table using one row

  13. 13

    Subtract rows of one table from another table

  14. 14

    Select unique pairs of rows from one table that are associated to the same value in another table in SQL

  15. 15

    Fetching unique rows from one table that columns reference has not in another table

  16. 16

    PostgreSQL for each row from one table join all rows from another table

  17. 17

    Insert multiple rows from select into another table

  18. 18

    insert multiple rows mysql from another table

  19. 19

    How to insert specific columns and rows of data to an existing table from one table to another?

  20. 20

    Insert multiple rows in one table based on number in another table

  21. 21

    How to insert multiple rows into one table for each id of another table

  22. 22

    Create trigger to insert primary key from one table upon creation of new row, into column of another table in MySQL

  23. 23

    Foreach id in one table, insert row in another table

  24. 24

    Link one row of table to multiple rows in another table in MySQL

  25. 25

    MySQL query - single row in one table with multiple rows in another table

  26. 26

    SELECT one row of a table, And all rows in another table

  27. 27

    Insert rows from one table to another but only those rows that have no duplicates

  28. 28

    SQL query INSERT SELECT COUNT from one table to another row by row

  29. 29

    Update and insert to one table from another

HotTag

Archive