简体   繁体   English

使用选择查询中的值作为新列的名称,将新列添加到现有TABLE中

[英]Add new columns into a existing TABLE using the values from a select query as the name of the new columns

I would like to use the values of a existing column in a table as the name of columns created into another existing table. 我想将表中现有列的值用作在另一个现有表中创建的列的名称。 At the same time insert the values achieved from another table into the new created columns. 同时,将从另一个表中获得的值插入到新创建的列中。 I have the following: Table: "feature_description" containing 4 columns. 我有以下内容:表:包含4列的“ feature_description”。 feature_id feature_language feature_name feature_admin_name 2 2 Laktosefrei: Laktosefrei .... there are 8 values under feature-admin_name. feature_id feature_language feature_name feature_admin_name 2 2 Laktosefrei:Laktosefrei .... feature-admin_name下有8个值。 I would like to add 8 new columns into a existing Table "products" named after these 8 values. 我想将8个新列添加到以这8个值命名的现有表“产品”中。 Until now I have the following Code. 到目前为止,我有以下代码。

<?php
if ($conn = mysqli_connect("127.0.0.1", "root", "xxxxxxx", "fk16"))
{   
$feature_index = "SELECT * 
                  FROM feature_set_values AS fsv  
             LEFT JOIN feature_value_description AS fvd ON fsv.feature_value_id = fvd.feature_value_id   
             LEFT JOIN feature_value AS fv ON fv.feature_value_id = fvd.feature_value_id    
             LEFT JOIN feature_description AS fd ON fd.feature_id = fv.feature_id    
             LEFT JOIN feature_set_to_products AS fstp ON fstp.feature_set_id = fsv.feature_set_id";
if ($filter_query = mysqli_query($conn, $feature_index))
{       
  while ($filter_name = mysqli_fetch_array($filter_query))
    {
        //$feature_name = array();
        $feature_name = $filter_name["feature_admin_name"];
        //$feature_value = array();
        $feature_value = $filter_name["feature_value_text"];
        for ($i = 0; $i < count($filter_name['feature_admin_name']); $i++){
        echo $filter_name['feature_admin_name'][$i]."\n<br />";
        mysqli_query($conn, "ALTER TABLE products ADD (".$filter_name['feature_admin_name'][$i]." VARCHAR(50))");
        }           
    }
    //echo "</table>";
}   
mysqli_close($conn);
}
else {
echo "My Fehler";
}
?>

But all I get are just 4 columns added into table "products" named with the Initials of the 8 values! 但是,我得到的只是添加到以8个值的缩写命名的表“产品”中的4列! The 8 values are: Laktosefrei, Glutenfrei, Gentechnikfrei, BIO, Herkunft, Milchsorte, Labart, Milchbehandlung. 这8个值分别是:Laktosefrei,Paladfrei,Gentechnikfrei,BIO,Herkunft,Milchsorte,Labart,Milchbehandlung。 But I get 4 columns with the names: L, G, H, M. What am I doing wrong? 但是我得到4个列,名称分别为:L,G,H,M。我做错了什么? Kann please somebody help me in this matter. 坎恩,请有人在这件事上帮助我。 Thankyou. 谢谢。 when "var_dump" used, then I get the "echo" result: Result of "var_dump" by "echo" 当使用“ var_dump”时,我得到“ echo”结果:“ echo” 的“ var_dump”结果

First of all, special thanks to Kishor, who gave me the right tip with var_dump() . 首先,特别感谢Kishor,他通过var_dump()给了我正确的建议。 I have now solved the problem as follows: 我现在已经解决了以下问题:

<?php
if ($conn = mysqli_connect("127.0.0.1", "root", "xxxxxx", "fk16"))
{
  $feature_name = array();
  $feature_index = "SELECT * 
                  FROM feature_set_values AS fsv  
             LEFT JOIN feature_value_description AS fvd ON fsv.feature_value_id = fvd.feature_value_id   
             LEFT JOIN feature_value AS fv ON fv.feature_value_id = fvd.feature_value_id    
             LEFT JOIN feature_description AS fd ON fd.feature_id = fv.feature_id    
             LEFT JOIN feature_set_to_products AS fstp ON fstp.feature_set_id = fsv.feature_set_id
               LEFT JOIN products AS p ON p.products_id = fstp.products_id";
if ($filter_query = mysqli_query($conn, $feature_index))
{
  while ($filter_name = mysqli_fetch_array($filter_query))
  {
    $feature_name = array('id' => $filter_name["feature_id"], 'name' => $filter_name['feature_admin_name']);
        foreach ($feature_name as $id => $name)
        {
          mysqli_query($conn, "ALTER TABLE products ADD (".$name." VARCHAR(20))");
        }
  }
  mysqli_close($conn);
}
?>

Now, I get the 8 necessary values from the table feature_description.feature_admin_name as the name of 8 new columns added into the table products as needed. 现在,我从表feature_description.feature_admin_name获取8个必要值,作为根据需要添加到表products的8个新列的名称。 Once again thankyou for your help and tips in this matter. 再次感谢您在此问题上的帮助和提示。

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

相关问题 在 Laravel 的迁移中向现有表添加新列 - Add new columns to existing table in a migration in Laravel 我想使用 SELECT 查询添加一个新列,并从 SQL 中的多个列中获取最新值 - I want to add a new column using SELECT query and get a latest value from multiple columns in SQL 使用另一个表中的列和新值“插入”语法 - “insert into” syntax using columns from another table and new values 使用 ALT 和 ADD 添加新列后如何向现有行添加值 - How to add values to the existing rows after adding new columns using ALT and ADD 如果两个现有列匹配,则SQL查询添加新列,并从另一列添加值 - SQL query add new column if two existing columns matches and add value from another column 从旧表创建新表并添加2个新列 - Create new table from old table and add 2 new columns 从具有多个自定义列的现有表中创建一个新表 - Create a new table from existing table with multiple custom columns 如何将n个列添加到现有表中,新列应具有现有列的聚合 - How to add n number of columns to an existing table,the new columns should have the aggregate of an existing column 插入现有表中新添加的列中 - inserting into new added columns in an existing table 查询以从不同的行中选择值,并将它们作为列添加到另一个表中 - Query to select values from different rows and add them as columns to another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM