Answer the question
In order to leave comments, you need to log in
How to transfer data from one MySql table to another, while updating in the first one?
Hello comrades! Once again, your help is needed!
There are 2 almost identical tables:
CREATE TABLE `queue` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) unsigned NOT NULL,
`product_id` int(11) unsigned NOT NULL,
`count` tinyint(3) unsigned NOT NULL,
`time` int(11) unsigned NOT NULL,
`state` enum('draft','pending','accepted') NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `user_id_product_id` (`user_id`,`product_id`)
) ENGINE=InnoDB AUTO_INCREMENT=20267 DEFAULT CHARSET=utf8;
CREATE TABLE `offers` (
`user_id` int(11) unsigned NOT NULL,
`product_id` int(11) unsigned NOT NULL,
`count` tinyint(3) unsigned NOT NULL,
`finish_time` int(11) unsigned NOT NULL,
PRIMARY KEY (`user_id`,`product_id`),
UNIQUE KEY `user_id_product_id` (`user_id`,`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
public static function executeQueue(array $keys = [])
{
$returnArray = [];
if (count($keys) > 0) {
$rows = Queue::find()->where([
'state' => Queue::STATE_PENDING,
'id' => $keys
])->all();
} else {
$rows = Queue::findAll(['state' => Queue::STATE_PENDING]);
}
foreach ($rows as $specialOffer) {
$record = new Offer();
$record->user_id = $specialOffer->user_id;
$record->product_id = $specialOffer->product_id;
$record->count = $specialOffer->count;
$record->finish_time = $specialOffer->time;
if ($record->validate() && $record->save()) {
$specialOffer->state = Queue::STATE_ACCEPTED;
$specialOffer->save();
$returnArray[] = $specialOffer->id;
}
}
return $returnArray;
}
INSERT INTO offers (
user_id,
product_id,
count,
finish_time
) SELECT
user_id,
product_id,
count,
time
FROM
queue
WHERE
state = 'pending'
Answer the question
In order to leave comments, you need to log in
1) Transfer data:
INSERT DELAYED IGNORE INTO offers (
user_id,
product_id,
count,
finish_time
) SELECT
user_id,
product_id,
count,
time
FROM
queue
WHERE
state = 'pending'
UPDATE queue
SET state = 'accepted'
WHERE
state != 'accepted'
AND (product_id, user_id) IN (
SELECT
product_id,
user_id
FROM
offers
)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question