red
red

Reputation: 1619

Mysql, insert based on condition of select

I need to execute complex query with insert based on select comparison of number of rows and value of that rows. Here what I want to do: - select from table "media" all the media with specific "post_id" - check if all rows have "media_status" set to active - if yes update in table "media" the value of "posts_status" to "active"

Is it possible to do it in one query?

async.waterfall([
        function download(next) {
            // pick media
            },
        function transform(response, next) {
            // transcode media
        },
        function upload(next) {
            // upload transcoded
            },            
        function clean(response, next) {
            // clean temp

            },
        function connectDb(next) {
            // connect to db
            },
        function saveDb(next) {
            // save single media
            },
        function conditionalNotification(next) {
            // HERE I NEED TO CHECK IF ALL MEDIA ARE TRANSCODED AND SET POST_STATUS ACTIVE 
            }             
            ...

Upvotes: 0

Views: 51

Answers (1)

Andrews B Anthony
Andrews B Anthony

Reputation: 1381

You can do this by executing this single query

update `media` set post_status='active' 
where 1=((select count(*) from `media` 
where post_id = <id value>)=
(select count(*) from `media` 
where post_id = <id value> and media_status='active'))

Upvotes: 1

Related Questions