[英]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.