gpt4 book ai didi

mysql - 复制表行,但使用另一个值

转载 作者:行者123 更新时间:2023-11-29 11:29:02 25 4
gpt4 key购买 nike

我有表product_to_store(它是opencart表),其中product_idstore_id作为列标题。表格中充满了数据(示例如下):

+------------+----------+
| product_id | store_id |
+------------+----------+
| 123 | 0 |
| 124 | 0 |
| 125 | 0 |
| 126 | 0 |
| 125 | 1 |
+------------+----------+

帮助(来自 mysql 表转储):

CREATE TABLE `product_to_store` (
`product_id` int(11) NOT NULL,
`store_id` int(11) NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO `product_to_store` (`product_id`, `store_id`) VALUES
(123, 0),
(124, 0),
(125, 0),
(126, 0),
(125, 1);

ALTER TABLE `product_to_store`
ADD PRIMARY KEY (`product_id`,`store_id`);

我想复制所有值为 0 的行作为 store_id。我希望它们的值不是 0,而是 1(例如)。我怎样才能使用 mysql 语法做到这一点?

所以决赛 table 应该是这样的

+------------+----------+
| product_id | store_id |
+------------+----------+
| 123 | 0 |
| 124 | 0 |
| 125 | 0 |
| 126 | 0 |
| 125 | 1 |
| 123 | 1 |
| 124 | 1 |
| 126 | 1 |
+------------+----------+

最佳答案

我认为这会对您有所帮助:

insert ignore into `product_to_store` (
select a.product_id, 1 as store_id
from (select distinct product_id from product_to_store where store_id=0) a
)

关于mysql - 复制表行,但使用另一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37728385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com