I asked this question yesterday:
Insert select with a twist question
Now i have the problem that i want the data to update itself if it is a duplicate row.
So i found that i could do it by doing this:
insert into product_quantity
(groupId, productId, quantity)
select 3, productId, quantity
from product_quantity
ON DUPLICATE KEY UPDATE
product_quantity.quantity = VALUES(product_quantity.quantity);
But i want the quantity to update itself by adding the quantity to the already existsing quantity.
So i want something like this:
ON DUPLICATE KEY UPDATE
product_quantity.quantity =
VALUES(product_quantity.quantity) + product_quantity.quantity;
So if i got:
id----groupId----productId----quantity
1 ----- 2 ------------2--------------5
2 ----- 3 ------------2--------------5
Where groupId and productId are unique.
And i do the Insert-select-duplicate query:
insert into product_quantity
(groupId, productId, quantity)
select 3, 2, 5
from product_quantity
ON DUPLICATE KEY UPDATE
product_quantity.quantity = VALUES(product_quantity.quantity) + *OLD QUANTITY*;
MySql should add the quantity on row 1 to row 2, so it would look like this:
id----groupId----productId----quantity
1 ----- 2 ------------2--------------5
2 ----- 3 ------------2--------------10
Anyone got an idea if this is possible?
INSERT
INTO product_quantity (groupId, productId, quantity)
VALUES (3, productId, quantity)
FROM product_quantity
ON DUPLICATE KEY
UPDATE quantity = 5 + VALUES(quantity);
I think this'll do what you need.
Related
So here I have multiple table and joined them but here I have put simple example like:
here i am able to find all those items who has category_id 1 and 3 any one using in query, but now i need and condition here like where category 1 and 3 both matches.
here i need all those items who has category_id 1 and 3 both.
So how to do that?
mysql in query is matching anyone exist, so is there any other function which matches multiple values?
Using INNER JOIN with a subquery to get the items that have both category_id 1 and 3. The subquery would select all the item_id's that have category_id 1 or 3 and the main query would join with the subquery on the item_id.
SELECT p1.item_id, p1.product_id, p1.category_id
FROM PRODUCT p1
INNER JOIN (
SELECT item_id
FROM PRODUCT
WHERE category_id IN (1, 3)
GROUP BY item_id
HAVING COUNT(DISTINCT category_id) = 2
) p2 ON p1.item_id = p2.item_id
WHERE p1.category_id IN (1, 3);
Sample:
-- create
CREATE TABLE PRODUCT (
id INTEGER PRIMARY KEY,
item_id INTEGER NOT NULL,
product_id INTEGER NOT NULL,
category_id INTEGER NOT NULL
);
-- insert
INSERT INTO PRODUCT VALUES (3862, 297,0, 1);
INSERT INTO PRODUCT VALUES (3861, 297,0, 3);
INSERT INTO PRODUCT VALUES (3860, 23, 0,1);
Output:
item_id product_id category_id
297 0 3
297 0 1
I have read that the function "values ()" is being removed in a future version - and I am therefore looking for an alternative - to if you have an "insert into select" command.
I have searched a lot on the web, but do not think I have found an answer - when it comes to "select" ...
Let's say I have 2 tables (t1 and t2) and some summations need to be written in t1 from t2.
t1:
product_id
quantity
total_value
1
3
100
2
2
80
t2:
product_id
value
1
50
1
50
1
50
2
40
2
50
Normally I would do it as follows:
insert into t1 (product_id,quantity,total_value)
select product_id,count(*),sum(value)
from t2 group by product_id on duplicate key update quantity=values(quantity),total_value=values(total_value)
This will give the following result:
t1:
product_id
quantity
total_value
1
3
150
2
2
90
But how do I do something similar if I can not use "values ()" ..?
It is of course an option to delete the relevant rows in t1 first - to avoid having a "duplicate key" at all - but is there no better way ..?
Thanks.
Normally I would do it as follows:
Use SELECT aliases:
insert into t1 (product_id, quantity, total_value)
select *
FROM ( SELECT product_id AS p, count(*) AS q, sum(value) AS s
from t2
group by p ) src
on duplicate key update quantity=q, total_value=s
But how do I do something similar if I can not use "values ()" ..?
The same
insert into t1 (product_id, quantity, total_value)
VALUES (1, 10, 100) AS x(p, q, s)
on duplicate key update quantity=q, total_value=s
fiddle
This question already has answers here:
MySQL error 1241: Operand should contain 1 column(s)
(3 answers)
Closed 4 years ago.
I've seen this with almost the same problems as me MySQL INSERT IF (custom if statements).
but I did not successfully execute this query:
INSERT into cart_items (product_id,product_variant_id,product_attribute_id,cart_id,quantity)
select (7,7,12,11,4)
where (select pa.stock from product_attribute as pa where pa.id = 12) > 4
in this case this is (7,7,12,11,4) value of this (product_id,product_variant_id,product_attribute_id,cart_id,quantity)
product_id: 7
product_variant_id :7
product_attribute_id : 12
cart_id : 11
quantity : 4
in my headiSQL i got error like this :
SQL (Error(1241) : Operand should contain 1 column(s)
Table cart_items
ID : Primary key autoincrement
product_variant_id : Foreign key
product_attribute_id : Foreign key
product_id : Foreign key
cart_id : Foreign key
quantity :int
Table product_attribute
ID : Primary key autoincrement
product_id : Foreign key
product_variant_id :Foreign key
size : varchar
stock : int
I want to make query if cart_items quantity less than
product_attribute stock than insert
Try selecting from the dummy DUAL table:
INSERT into cart_items (product_id, product_variant_id, product_attribute_id,
cart_id, quantity)
SELECT 7, 7, 12, 11, 4
FROM dual
WHERE (SELECT pa.stock FROM product_attribute pa WHERE pa.id = 12) > 4;
I don't know if the above logic is what you actually intend to run, but it at least should run without syntax errors.
INSERT into cart_items (product_id,product_variant_id,product_attribute_id,cart_id,quantity)
SELECT product_id,product_variant_id,product_attribute_id,cart_id,quantity FROM product_attribute
WHERE id = ? && (SELECT stock from product_attribute WHERE id = ?) > (SELECT quantity FROM cart_items WHERE product_id = ?)
This makes sure that the stock amount is more than the cart quantity. Make sure the product ids match (?)
Also, I am not sure if you meant to do an INSERT from SELECT, but because you provided raw integer values in your example, I have changed it to be INSERT (columns) VALUES (values) because that's the correct format based on your example.
I wrote a query that is throwing an error when it can't find a record:
Error
Column 'product_id' cannot be null
MySQL
INSERT INTO orders (date, product_id, quantity)
VALUES ('11/29/2012', (
SELECT product_id FROM products WHERE product_name = 'Oranges'
), 12)
;
I'm actually iterating in my PHP and some of the product_name records are not going to exist.
Can I say somehow that if the subquery returns nothing, gracefully stop/abort so the PHP can keep iterating?
Try this:
INSERT INTO orders (date, product_id, quantity)
SELECT '11/29/2012', product_id, 12
FROM products
WHERE product_name = 'Oranges'
If there is no matching product, the query will succeed but return no rows modified. If you wish you can read the number of rows modified from PHP when you execute the query.
Notice that if there are multiple products with product_name = 'Oranges' you'll get multiple rows inserted into your table.
If you want a record to be inserted anyway, you can try to fallback to e.g. 0 if no such product exists:
INSERT INTO orders (date, product_id, quantity)
VALUES ('11/29/2012', COALESCE((
SELECT product_id FROM products WHERE product_name = 'Oranges'
), 0), 12);
what's the easiest way of to clone a row with a different ID in MySQL.
For example:
Products
product_id name price
------------------------
1 a 10
2 b 15
It looks like weird, but I need to clone product with id = 1. So the table will look like:
Products
product_id name price
------------------------
1 a 10
2 b 15
3 a 10
You can use subqueries:
INSERT INTO
donation (name,price)
SELECT name,price
FROM donation
WHERE product_id = 1
INSERT INTO Products (name, price) VALUES ((SELECT name FROM Products WHERE product_id = 1), (SELECT price FROM Products WHERE product_id = 2));
INSERT INTO Products (name, price)
VALUES
((SELECT name, price
FROM Products
WHERE product_id = 1));
If you want to clone a row with a single SQL statement and can't or don't want to list all the values but there is a primary key you can use:
INSERT INTO Products
SELECT *
FROM Products
WHERE product_id = 1
UNION ALL
SELECT *
FROM Products
WHERE product_id = 1
ON DUPLICATE KEY UPDATE product_id=(SELECT MAX(product_id)+1 FROM Products);
This tries to insert two copies of the row into the databse and when the first row insert fails due to a duplicate product_id it uses ON DUPLICATE KEY to update the existing row in the table to the next available product_id, which leaves the original product_id available for when the second row is inserted.