SQL如何计算数据库中同一行的出现数量

发布于 2025-02-12 13:47:24 字数 244 浏览 4 评论 0原文

我的数据在mySQL表中:

Cart_id    Product_id    Quantity
 1           1             1

我可以以某种方式做到这一点:当我添加datas的下一个行时: cart_id 1和product_id 1和数量1,新行将不会插入,但是第一行将更新为2的数量?

但是,当我插入数据:cart_id 1和product_id 2时,将插入新行。

My data in mySql table:

Cart_id    Product_id    Quantity
 1           1             1

Can i do somehow, that acction: when i add next row with datas:
cart_id 1 and product_id 1 and quantity 1 , the new row will be not inserted, but the first row will update quantity to 2?

But when i insert datas: cart_id 1 and product_id 2, the new row will be inserted.

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

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

发布评论

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

评论(1

旧伤慢歌 2025-02-19 13:47:24

使用这样的查询:

INSERT INTO myTable (Cart_id,Product_id,Quantity) VALUES( 1,1,1)
ON DUPLICATE KEY UPDATE Quantity=Quantity+VALUES(Quantity);

在这里示例:

创建一个表:

CREATE TABLE `myTable` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `Cart_id` int(11) DEFAULT NULL,
  `Product_id` int(11) DEFAULT NULL,
  `Quantity` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

添加唯一索引:

ALTER TABLE `myTable` ADD UNIQUE INDEX (`Cart_id`, `Product_id`);

您还可以在创建创建索引时指导创建索引表

插入行样本:

MariaDB [bernd]> INSERT INTO myTable (Cart_id,Product_id,Quantity) VALUES( 1,1,1)
    -> ON DUPLICATE KEY UPDATE Quantity=Quantity+VALUES(Quantity);
Query OK, 1 row affected (0.003 sec)

MariaDB [bernd]> SELECT * from myTable;
+----+---------+------------+----------+
| id | Cart_id | Product_id | Quantity |
+----+---------+------------+----------+
|  1 |       1 |          1 |        1 |
+----+---------+------------+----------+
1 row in set (0.000 sec)

MariaDB [bernd]> 
MariaDB [bernd]> INSERT INTO myTable (Cart_id,Product_id,Quantity) VALUES( 1,1,1)
    -> ON DUPLICATE KEY UPDATE Quantity=Quantity+VALUES(Quantity);
Query OK, 2 rows affected (0.001 sec)

MariaDB [bernd]> SELECT * from myTable;
+----+---------+------------+----------+
| id | Cart_id | Product_id | Quantity |
+----+---------+------------+----------+
|  1 |       1 |          1 |        2 |
+----+---------+------------+----------+
1 row in set (0.001 sec)

MariaDB [bernd]> INSERT INTO myTable (Cart_id,Product_id,Quantity) VALUES( 1,1,1)
    -> ON DUPLICATE KEY UPDATE Quantity=Quantity+VALUES(Quantity);;
Query OK, 2 rows affected (0.001 sec)


MariaDB [bernd]> SELECT * from myTable;
+----+---------+------------+----------+
| id | Cart_id | Product_id | Quantity |
+----+---------+------------+----------+
|  1 |       1 |          1 |        3 |
+----+---------+------------+----------+
1 row in set (0.000 sec)

MariaDB [bernd]> INSERT INTO myTable (Cart_id,Product_id,Quantity) VALUES( 1,1,5)
    -> ON DUPLICATE KEY UPDATE Quantity=Quantity+VALUES(Quantity);
Query OK, 2 rows affected (0.002 sec)

MariaDB [bernd]> SELECT * from myTable;
+----+---------+------------+----------+
| id | Cart_id | Product_id | Quantity |
+----+---------+------------+----------+
|  1 |       1 |          1 |        8 |
+----+---------+------------+----------+
1 row in set (0.000 sec)

MariaDB [bernd]> 

Use a Query like this:

INSERT INTO myTable (Cart_id,Product_id,Quantity) VALUES( 1,1,1)
ON DUPLICATE KEY UPDATE Quantity=Quantity+VALUES(Quantity);

Here a Sample:

create a Table:

CREATE TABLE `myTable` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `Cart_id` int(11) DEFAULT NULL,
  `Product_id` int(11) DEFAULT NULL,
  `Quantity` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

add the unique index:

ALTER TABLE `myTable` ADD UNIQUE INDEX (`Cart_id`, `Product_id`);

you can also direct create the index when creating the table

insert rows sample:

MariaDB [bernd]> INSERT INTO myTable (Cart_id,Product_id,Quantity) VALUES( 1,1,1)
    -> ON DUPLICATE KEY UPDATE Quantity=Quantity+VALUES(Quantity);
Query OK, 1 row affected (0.003 sec)

MariaDB [bernd]> SELECT * from myTable;
+----+---------+------------+----------+
| id | Cart_id | Product_id | Quantity |
+----+---------+------------+----------+
|  1 |       1 |          1 |        1 |
+----+---------+------------+----------+
1 row in set (0.000 sec)

MariaDB [bernd]> 
MariaDB [bernd]> INSERT INTO myTable (Cart_id,Product_id,Quantity) VALUES( 1,1,1)
    -> ON DUPLICATE KEY UPDATE Quantity=Quantity+VALUES(Quantity);
Query OK, 2 rows affected (0.001 sec)

MariaDB [bernd]> SELECT * from myTable;
+----+---------+------------+----------+
| id | Cart_id | Product_id | Quantity |
+----+---------+------------+----------+
|  1 |       1 |          1 |        2 |
+----+---------+------------+----------+
1 row in set (0.001 sec)

MariaDB [bernd]> INSERT INTO myTable (Cart_id,Product_id,Quantity) VALUES( 1,1,1)
    -> ON DUPLICATE KEY UPDATE Quantity=Quantity+VALUES(Quantity);;
Query OK, 2 rows affected (0.001 sec)


MariaDB [bernd]> SELECT * from myTable;
+----+---------+------------+----------+
| id | Cart_id | Product_id | Quantity |
+----+---------+------------+----------+
|  1 |       1 |          1 |        3 |
+----+---------+------------+----------+
1 row in set (0.000 sec)

MariaDB [bernd]> INSERT INTO myTable (Cart_id,Product_id,Quantity) VALUES( 1,1,5)
    -> ON DUPLICATE KEY UPDATE Quantity=Quantity+VALUES(Quantity);
Query OK, 2 rows affected (0.002 sec)

MariaDB [bernd]> SELECT * from myTable;
+----+---------+------------+----------+
| id | Cart_id | Product_id | Quantity |
+----+---------+------------+----------+
|  1 |       1 |          1 |        8 |
+----+---------+------------+----------+
1 row in set (0.000 sec)

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