使用 mySQL 和 PHP 从一张表中检索两列插入另一张表

发布于 2024-11-07 18:47:45 字数 694 浏览 6 评论 0原文

我在 mySQL 中有两个表,特别是帐户和订阅者数据。

我想使用帐户表中存在的两列(id,名称),并将其插入到subscriber_data表中名为(sub_id,value)的两列中。

我似乎无法找到一种方法来检索此信息并将其放入另一个表中,而不会将其集中在同一行的同一值字段中,如下所示: 0, 0, 1

目前,我的 PHP 如下所示

$sql = "SELECT * FROM accounts";
$result = mysql_query($sql);

while($alt_result = mysql_fetch_array($result, MYSQL_NUM)){
$id[] = $alt_result[0];
$value1[] = $alt_result[1];
}
$idstring = implode(', ', $id); 
$value1 = implode(', ', $value1); 


$sql = "INSERT INTO subscriber_data (field_id, sub_id, value) VALUES('1', '$idstring', '$value1') ON DUPLICATE KEY UPDATE value='$value1'";
$result = mysql_query($sql);

:这是一个新手,显然做错了什么......我搜索但找不到答案。

谢谢。

I have two tables in mySQL, notably accounts and subscriber_data.

I want to use two columns present in the accounts table (id, name), and insert it to the subscriber_data table in two columns called (sub_id, value).

I cannot seem to find a way to retrieve this information and put it into the other table without it being bunched up in the same value field in the same row like this: 0, 0, 1

Currently, my PHP looks like this:

$sql = "SELECT * FROM accounts";
$result = mysql_query($sql);

while($alt_result = mysql_fetch_array($result, MYSQL_NUM)){
$id[] = $alt_result[0];
$value1[] = $alt_result[1];
}
$idstring = implode(', ', $id); 
$value1 = implode(', ', $value1); 


$sql = "INSERT INTO subscriber_data (field_id, sub_id, value) VALUES('1', '$idstring', '$value1') ON DUPLICATE KEY UPDATE value='$value1'";
$result = mysql_query($sql);

I am a novice at this, and obviously doing something wrong... I searched and couldn't find an answer.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江城子 2024-11-14 18:47:45

在调用插入脚本之前,我一开始并没有看到你在 PHP 中做什么。也许是因为我不习惯 PHP 脚本,但更可能是因为我当时没有给予足够的重视。

因此,据我了解您在做什么,您似乎做错了。

如果需要将一个表中的值插入到另一表中,只需使用 INSERT...SELECT 构造即可。

在您的情况下,它可能应该是这样的:

INSERT INTO subscriber_data (field_id, sub_id, value)
SELECT '1', id, name
FROM accounts

请注意,这是一条指令,它替换代码中的 SELECT...INSERT... 。我认为如果只保留上面 PHP 脚本的最后两行就足够了,其中存储到 $sql 的 SQL 脚本只需根据我的建议进行修改即可。

如果我遗漏了什么,请告诉我。

I didn't see at first what you were doing in PHP before calling the insert script. Maybe because I'm not used to PHP scripting, though more probably because I didn't pay enough attention at the time.

So, as far as I can understand what you are doing, you seem to be doing it wrong way.

If you need to insert values from one table into another table, you just use the INSERT...SELECT construct.

In your case it should possibly be something like this:

INSERT INTO subscriber_data (field_id, sub_id, value)
SELECT '1', id, name
FROM accounts

Note that this is one instruction, and it replaces both SELECT... and INSERT... in your code. I think it will be enough if you leave only the last two lines of your PHP script above, where the SQL script you are storing to $sql should simply be modified according to my suggestion.

If I'm missing something, please let me know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文