I've got an INSERT ... ON DUPLICATE KEY UPDATE query and I am trying to add WHERE clause to it:
INSERT INTO `product_description` (
`product_id`,`language_id`,`name`,
`description`,`meta_description`,
`meta_keyword`,`tag`
) VALUES (
$getProductId, $languageId, '$pName', '$pDescription', '', '', ''
)
ON DUPLICATE KEY UPDATE
`name` = '$pName',
`description` = '$pDescription'
I want to restrict the UPDATE to those 2 conditions:
WHERE `model` = 'specific-model' AND `sku` NOT LIKE '%B15%'
If I add this part of query to the end of the original query I get a MySQL syntax error. What would be a working solution?
Update: Please note that model and sku are in another table, and the common key is product_id
I would suggest you to use some sort of prepared statement instead of concatenating strings, so you should do something like this:
INSERT INTO `product_description` (
`product_id`, `language_id`, `name`,
`description`, `meta_description`,
`meta_keyword`, `tag`
) VALUES (?, ?, ?, ?,'','','')
but this is not part of the question.
I was thinking of answering with a simple CASE WHEN but the challenging part of your question is that the restrict conditions are not in the product_description table but are from another table. So I think we can just use a TRIGGER:
CREATE TRIGGER product_description_upd
BEFORE UPDATE ON product_description
FOR EACH ROW
IF NOT EXISTS(SELECT * FROM models
WHERE product_id=new.product_id
AND model='Abc' AND `sku` NOT LIKE '%B15%') THEN
SET new.name=old.name;
SET new.description=old.description;
END IF;
//
then you can use an INSERT query like:
INSERT INTO `product_description` (col1, col2, ...)
VALUES (..., ..., ...)
ON DUPLICATE KEY
UPDATE name=VALUE(name),description=VALUE(description)
Please see a fiddle here.
The only thing to note here is that even a standard UPDATE query will be affected.
CREATE TABLE product_description (
product_id INT PRIMARY KEY,
name VARCHAR(100),
description VARCHAR(100)
);
CREATE TABLE models (
product_id INT,
model VARCHAR(100),
sku VARCHAR(100)
);
INSERT INTO models VALUES
(1, "Abc", "ZZZ"),
(2, "Abc", "B15");
INSERT INTO product_description VALUES
(1, "Car", "Red"),
(2, "Truck", "Pink");
INSERT INTO `product_description` VALUES (1, "NewCar", "DeepRed")
ON DUPLICATE KEY UPDATE name=VALUES(name), description=VALUES(description);
Assuming, product_id must be in models.
INSERT INTO `product_description` (product_id, name, description)
SELECT models.product_id, "SuperCar" as name, "DarkRed" as description
FROM `models` WHERE model="Abc" AND `sku` NOT LIKE "%B15%"
ON DUPLICATE KEY UPDATE name="UpdatedCar", description="UpdatedRed";
refer to http://sqlfiddle.com/#!9/69624e/1
Hopefully this solves the problem. You can play with SELECT query for different result.
Related
In MySQL, is it possible to do an upsert but only set a column value on insert (and not set the column value on update).
For example, for a createdBy column, we only want to set the value on insert, we don't want to override that value on update (because we lose who originally inserted the column).
Note that we only know the currently logged in user. So updatedBy is simple -- always use the value of the logged in user. But createdBy is hard. Use the value of the logged in user but only for an insert -- don't override this on update.
Example schema:
CREATE TABLE `movie` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`name` NVARCHAR(100) NOT NULL,
`createdBy` NVARCHAR(100) NOT NULL,
`updatedBy` NVARCHAR(100) NOT NULL,
UNIQUE INDEX (`name`)
);
Example of a standard upsert:
INSERT INTO `movie` (`name`, `createdBy`, `updatedBy`)
VALUES ('The Matrix', 'Jill', 'Jill')
ON DUPLICATE KEY UPDATE
`id` = LAST_INSERT_ID(`id`),
`name` = VALUES(`name`),
`createdBy` = VALUES(`createdBy`),
`updatedBy` = VALUES(`updatedBy`)
;
Here's my attempt to only set the createdBy column on insert using IFNULL. But this doesn't work and results in createdBy always being null.
INSERT INTO `movie` (`name`, `createdBy`, `updatedBy`)
VALUES ('The Matrix', IFNULL(`createdBy`, 'Jill'), 'Jill')
ON DUPLICATE KEY UPDATE
`id` = LAST_INSERT_ID(`id`),
`name` = VALUES(`name`),
`createdBy` = VALUES(`createdBy`),
`updatedBy` = VALUES(`updatedBy`)
;
Results wanted:
Case 1: Jill runs an upsert that inserts a row.
id = 1
name = 'The Matrix'
createdBy = 'Jill' // Created by Jill
updatedBy = 'Jill' // Last updated by Jill
Case 2: Bob runs an upsert that updates the same row.
id = 1
name = 'The Matrix Reloaded'
createdBy = 'Jill' // Created by Jill (do not change value on update)
updatedBy = 'Bob' // Last updated by Bob
I created a fiddle guessing that Name is the Key, feel free to give it a try here.
This is the basic syntax:
INSERT INTO `movie` (`name`, `UpdatedBy`,`CreatedBy`)
VALUES ('Star wars', 'NameA','NameB')
ON DUPLICATE KEY UPDATE `UpdatedBy` = VALUES(`UpdatedBy`)
;
Notice: NameA and NameB can be the same so you dont get nulls on inserts
Hope it helps :)
Try this:
INSERT INTO `movie` (`name`, `createdBy`)
VALUES ('The Matrix', 'Jill')
ON DUPLICATE KEY UPDATE `name` = VALUES(`name`)
;
I convert an id which is in a char column datatype. after that, I want to add it by 1 (plus 1).
Could you help me? why my query is not working?
query:
INSERT INTO `countries` (`id`, `country_name`) VALUES ((SELECT MAX(CAST(`id` as INTEGER)) AS `max_id` FROM `countries`) + 1, 'India');
The following would run:
INSERT INTO `countries` (`id`, `country_name`)
SELECT MAX(CAST(`id` as INTEGER)) + 1, 'India'
FROM `countries`;
But I think it would be easier if you just make the id column an AUTO_INCREMENT.
This is not how you should be doing identifiers.
If you want incrementing id values, you want to use the AUTO_INCREMENT feature when creating your table.
Your way is dangerous, there's always a possibility of two transactions running at the same time picking the same "next ID".
Just create a table with the flag on:
CREATE TABLE countries (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (id)
);
INSERT INTO countries (`name`) VALUES ('India');
Table Order
oid payerName address
1 james 1 brown
2 smith 2 smith
Table order_item
oid type price
1 AN94 3000
2 AK47 1000
order_item as a foreign from Order.
oid is an auto increment in Order Table
but in order_item table it is not(dont know if thats is the right way to do it)
I have an insert statement which inserts into both tables at the same time. I was wonder if it the right to make order_item oid an auto increment as well? because the is not other way I can make it copy the oid from order table.
What is the best approach to this small issue.
You shouldn't make order id field in order_items table to be auto_increment. To obtain a value of auto generated id when you insert a row in orders table use LAST_INSERT_ID() function.
It's OK to have it's own auto_increment id column in order_items. Sometimes it comes very handy (e.g. when you want to update an individual order_item row you can reference it by its own id rather than by a combination of columns).
That being said proposed schema might look like
CREATE TABLE orders
(`order_id` int not null auto_increment primary key,
`payerName` varchar(5),
`address` varchar(8)
);
CREATE TABLE order_items
(`order_item_id` int not null auto_increment primary key,
`order_id` int,
`type` varchar(4),
`price` decimal(19,2),
foreign key (order_id) references orders (order_id)
);
Names of id columns has been intentionally renamed in my example for clarity. You don't have to change yours obviously.
Now to insert an order and an order item you do
INSERT INTO orders (`payerName`, `address`)
VALUES ('james', '1 brown');
INSERT INTO order_items (`order_id`, `type`, `price`)
VALUES (LAST_INSERT_ID(), 'AN94', 3000);
Here is SQLFiddle demo
You didn't mention PDO in your question but your code using it might look something like this
try {
$db = new PDO('mysql:host=localhost;dbname=test', 'user', 'userpwd');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
//$db->beginTransaction();
$query=$db->prepare("INSERT INTO orders (`payerName`, `address`) VALUES (?, ?)");
$query->execute(array('james', '1 brown'));
$order_id = $db->lastInsertId();
$query=$db->prepare("INSERT INTO order_items (`order_id`, `type`, `price`) VALUES (?, ?, ?)");
$query->execute(array($order_id, 'AN94', 3000));
//$db->commit();
} catch (PDOException $e) {
echo "Exeption: " .$e->getMessage();
}
$query = null;
$db = null;
Since order_item->oid depends on order->oid, Order should exist first before the order_item
This one is right because the oid(1) exist in the order table
insert into order_item (oid,type,price) values('1','AN94','3000')
This one is wrong because the oid(3) doesn't exist in the order table
insert into order_item (oid,type,price)values('3','sample','3333')
BTW the table order-oid should be a primary key
Orders
order_id(auto inc), payer_name, address
Order Items
order_item_id(auto inc), order_id, item_id, type, quantity, unit_price
Product
item_id(auto inc), item_name, item_unit_price, item_group
CREATE TABLE IF NOT EXISTS `MyTable` (
`ID` SMALLINT NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO MyTable (ID,Name) VALUES (ID=4,Name='xxx')
or
INSERT INTO MyTable (Name) VALUES (Name='xxx')
The problem is that both INSERT statements produce the entry (4,0). Why 0 instead of "xxx"?
UPDATE: Primary key changed.
This should do the job :
INSERT INTO MyTable (ID, Name) VALUES (4, 'xxx')
I'm pretty sure it would be something like this, instead...
INSERT INTO MyTable (Name) VALUES ('xxx')
No need for the Name= part, since you've already specified which column you wish to insert into with the first (Name) definition.
Because the expression Name='xxx' is false, hence evaluates as zero.
You use the column=expression method use in on duplicate key update clauses as described here, not in the "regular" section of inserts. An example of that:
insert into mytable (col1,col2) values (1,2)
on duplicate key update col1 = col1 + 1
You should be using the syntax:
INSERT INTO MyTable (ID,Name) VALUES (4,'xxx')
Is that syntax of Name='xxx' valid? Never seen it before, i assume it is seeing it as an unquoted literal, trying to convert it to a number and coming up with 0? I'm not sure at all
Try this:
INSERT INTO MyTable (Name) VALUES ('xxx')
This is because you should mention the name of the column in the values part. And also because you do not define you primary key correctly (airlineID is not part of the field list)
CREATE TABLE IF NOT EXISTS `MyTable` (
`ID` SMALLINT NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO MyTable (ID,Name) VALUES (4,'xxx')
INSERT INTO MyTable (Name) VALUES ('xxx')
Try this
INSERT INTO MyTable (ID,Name) VALUES (4,xxx)
For more Info just visit this link
This may have a really easy answer. I have done much database stuff for a while. I am trying to get the auto_increment value from one table inserted into the value on another table. is there an easy way of doing this. For eg i have done:
CREATE TABLE table_a (
id int NOT NULL AUTO_INCREMENT,
a_value varchar(4),
PRIMARY KEY (id)
);
CREATE TABLE table_b (
id int NOT NULL,
b_value varchar(15),
FOREIGN KEY (id) REFERENCES table_a (id)
);
Now i want to insert values into the table but I would like 'id' values for table_a and table_b to be the same. So far i have:
INSERT INTO table_a VALUES (NULL, 'foobar');
But I do not know how to go about extracting the auto_incermented 'id' number from table_a into the 'id' value of table_b. I have looked at SELECT #id = LAST_INSERT_ID() but can not get it to work.
You cannot do that at once. You'll have to first insert into the first table:
INSERT INTO table_a (a_value) VALUES ('foobar');
and then insert into the second using the generated id:
INSERT INTO table_b (id, b_value) VALUES (##IDENTITY, 'foobar');
LAST_INSERT_ID() and no need for the select statement part.