简体   繁体   English

如何将值从数据库中的所有表插入一个表

[英]How to Insert values into one table from all tables from database

I have problems with the last line of the code: 我对代码的最后一行有疑问:

if(isset($_POST['kolona']))
{
foreach($_POST['kolona'] as $vrednost)
mysql_query("ALTER TABLE tablica ADD $vrednost text NOT NULL");
mysql_query("INSERT INTO tablica ( ".(implode(',',($_POST['kolona']))).") SELECT ".(implode(',',($_POST['kolona'])))." FROM druga");
}            

First query is making columns in table 'tablica' and second query suppose to insert values in that columns from all tables from which are the columns, for now it's just hard coded, it's only from table 'druga', but i don't know how to go through the all tables, not just 'druga'. 第一个查询是在表'tablica'中创建列,第二个查询假定是在所有表中的列中插入值,而这只是硬编码,仅来自表'druga',但我不知道如何浏览所有表格,而不仅仅是“ druga”。 I tried with a loop and also with implode function but nothing seems to be working. 我尝试使用循环以及爆破功能,但似乎没有任何效果。 Can anyone help? 有人可以帮忙吗?

You can tackle merging problems like this using the ON DUPLICATE KEY feature: 您可以使用ON DUPLICATE KEY功能解决合并问题:

INSERT INTO target (id, column1, column2)
  SELECT id, column1, column2 FROM source
  ON DUPLICATE KEY UPDATE column1=VALUES(column1), column2=VALUES(column2)

It works well provided you have a PRIMARY KEY column without conflicts between your source and target tables. 如果您有一个PRIMARY KEY列,并且在源表和目标表之间没有冲突,则它可以很好地工作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM