简体   繁体   English

MySQL查询-获取未插入的ID以向表中插入新行

[英]MySQL query - getting not inserted ids to insert new row to a table

I have two tables 我有两张桌子

the first one is process 第一个是过程

工艺表

and the second one is medialist 第二个是媒体列表

媒体列表表

I want to insert a new row to process but I need a medialist.id which is not inserted by a user before. 我想插入一个新行来处理,但是我需要一个medialist.id,而用户以前没有插入过。

select medialist_id from process where user_id=648 -> this returns the inserted rows. 从进程中选择medialist_id,其中user_id = 648->这将返回插入的行。

I want those rows excluded from medialist table and get a random medialist id to insert new row to medialist table. 我希望将这些行从medialist表中排除,并获得一个随机的medialist ID,以将新行插入到medialist表中。

What is the best cost efficient way to do this? 做到这一点的最佳成本效益方法是什么?

INSERT INTO process (user_id,ads_id,medialist_id,`status`) SELECT 'your_uid','your_ads_id',id,'your_status' FROM medialist WHERE id NOT IN (SELECT medialist_id FROM process) ORDER BY RAND() LIMIT 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM