简体   繁体   中英

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.

select medialist_id from process where user_id=648 -> this returns the inserted rows.

I want those rows excluded from medialist table and get a random medialist id to insert new row to medialist table.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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