开发者

How can I SELECT+INSERT in the same mysql query?

开发者 https://www.devze.com 2023-01-15 03:08 出处:网络
I have a php script that process keywords from a mysql table \"keywords\" (columns: id - keyword) and then it saves data into another table \"data\" (column: id[foreign key keywords.id] - dataname - d

I have a php script that process keywords from a mysql table "keywords" (columns: id - keyword) and then it saves data into another table "data" (column: id[foreign key keywords.id] - dataname - datavalue).

My p开发者_开发百科roblem is that when the script is ready to save the data I only have the keyword and not the id.

So is there a way I can get the keyword id and save the data in one mysql query? (i mean without having to do something like SELECT id from keywords where keyword = keyword, and then run another query for the INSERT.


If selecting and modification tables are different - you can use simple nested query:

INSERT INTO `data` (`id`, `dataname`) VALUES
(
    (SELECT `id` FROM `keywords` WHERE `keyword` = 'keyworrrrrd'),
    'blabla'
)

or

INSERT INTO `data` (`id`, `dataname`)
SELECT `id`, 'blabla' FROM `keywords` WHERE `keyword` = 'keyworrrrrd'
0

精彩评论

暂无评论...
验证码 换一张
取 消