Mysql,根据select的条件插入

标签 mysql node.js

我需要根据行数和该行值的选择比较来执行插入的复杂查询。 这是我想做的: - 从表“media”中选择具有特定“post_id”的所有媒体 - 检查所有行是否将“media_status”设置为事件状态 - 如果是,则将表“media”中的“posts_status”值更新为“active”

是否可以在一个查询中完成此操作?

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 
            }             
            ...

最佳答案

您可以通过执行这个单个查询来做到这一点

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'))

关于Mysql,根据select的条件插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57089659/

相关文章:

c# - 使用MYSQL数据库时Request.IsAuthenticated false : MVC5

mysql - INSERT 重复键后检索自动增量值

php - 在 sql 文件的 csv 转储中替换\N

javascript - 如何在 native react 中单击平面列表项时将值传递给模态并显示模态

php - 创建用户聊天系统 -(PHP、MySQL、AJAX?)

mysql - 向现有表添加外键时出错

node.js - 仅在第一次加载 Angular 网页时使用 Safari 接收 401 状态

javascript - npm update 未更新所有过时的模块

linux - 如何设置 Linux 环境变量,以便 pm2 可以看到它们用于 node.js 应用程序?

mongodb - Node.js mongoose : how to use the . in 和 .sort 查询的方法?