Massimiliano Rubino
Massimiliano Rubino

Reputation: 289

Mysql add an existing column to another table

I have several tables in a database. In table products_info one of the column is named short_description (Short description of a product) and has some values

I need the value present in the short_description

In table order-items I need to add the short_description column with the data from short_description present in Table products_info.

enter image description here

Any suggestions? Thank you.

Upvotes: 4

Views: 1304

Answers (3)

Maksym Fedorov
Maksym Fedorov

Reputation: 6456

Instead of the unnecessary copy of the column, you can combine products_info table and products_info table with JOIN statement. For example:

SELECT order_items.*,
       products_info.short_description
FROM order_items
LEFT JOIN products_info ON order_items.product_id = products_info.product_id

You shouldn't create any table in a database, you'll get data from this query in your PHP script.

You can use a result of the query in the following way:

<?php 
$sql = "SELECT lcqu_orders_items.*, lcqu_products_info.short_description FROM lcqu_orders_items LEFT JOIN lcqu_products_info ON lcqu_orders_items.product_id = lcqu_products_info.product_id" 
// run query 
?>

<?php foreach ($order['items'] as $item) { ?> 
    <tr> 
        <td><?php echo (float)$item['quantity']; ?></td> 
        <td><?php echo $item['sku']; ?></td> 
        <td><?php echo $item['sku']; ?></td> 
        <--! showing short description -->
        <td><?php echo $item['lcqu_products_info.short_description']; ?></td> 
        <td style="white-space: normal;"><?php echo $item['name']; ?> 
        <?php if (!empty($item['options'])) { 
           foreach ($item['options'] as $key => $value) {
               echo '<br />- '.$key .': '. $value;
          } 
        }?>
     <!-- another code -->
}?>

Upvotes: 3

Sathiraumesh
Sathiraumesh

Reputation: 6117

First Create a column named short_description in the table you want enter image description here

After that write a small php code for getting the "product_id" and the "short_description" from the "icqu_products_info table" and use the "product_id" to update the values in the "icu_orders_items" table

Upvotes: -1

Nic3500
Nic3500

Reputation: 8611

You have two tables in the question, with different details on the same products. One thing you never want to do is duplicate data across tables. It is wasteful of resources and risks the integrity of the data. Ex. if the name of a product changes, you will have to modify it in many tables. If you miss one, the application will have issues since it will not always receive the same name. You can read about data normalization.

To perform queries on two tables, you use a JOIN operation. One value that is unique to each product, and appears in both tables is product_id.

Ex, get the products name from lcqu_products_info

SELECT name
FROM lcqu_products_info

Ex, get the price from lcqu_orders_items

SELECT price
FROM lcqu_orders_items

Now if you need the name AND the price in a single query, join the tables with the product_id:

SELECT pi.name, oi.price
FROM lcqu_products_info AS pi
LEFT JOIN lcqu_orders_items AS oi ON pi.product_id = oi.product_id

Based on that, you should remove the "name" column from the lcqu_orders_items. The name should be extracted from the lcqu_products_info, using the same methos showed above.


Now about your PHP code:

<?php 
    foreach ($order['items'] as $item) 
    { 
?> 
    <tr>
        <td>
            <?php echo (float)$item['quantity']; ?>
        </td> 
        <td>
            <?php echo $item['sku']; ?>
        </td> 
        <td style="white-space: normal;">
            <?php echo $item['name']; ?> 
            <?php 
                if (!empty($item['options'])) 
                { 
                    foreach ($item['options'] as $key => $value) 
                    { 
                        echo '<br />- '.$key .': '. $value; 
                    } 
                } 
            ?>

In this code you require these values from lcqu_orders_items

  • quantity: in the table
  • sku: in the table
  • name: in the table. Should not be. It should be taken from lcqu_products_info
  • options: in the table.

Therefore the query should be:

SELECT oi.quantity, oi.sku, pi.name, oi.options
FORM lcqu_orders_items AS oi
LEFT JOIN lcqu_products_info AS pi ON oi.product_id = pi.product_id

the results of this query will end up into the $orders array, on which you can loop and print the data.


I understand this is not a direct answer to your question, but such an answer would send you down a path that is not best practice, and cause you problems later.

Upvotes: 1

Related Questions