在nodejs中从mysql中的左连接查询中选择

t3irkdon  于 2021-06-23  发布在  Mysql
关注(0)|答案(2)|浏览(429)

我试图用以下信息创建一个json对象
table
电影:主键电影ID
参与者:主键actorid
acted in:电影和演员的m到n关系的中间表主键=movieid和actorid。
这就是我要做的。
选择actorid=1的参与者
查询:

SELECT * FROM actors WHERE ActorID=1.

选择演员演过的所有电影。
查询:

SELECT * 
  from movies 
  LEFT 
  JOIN 
    ( SELECT *
       from actedin 
  WHERE ActorID = '+actorID+'
  ) AS actors_temp 
    ON movies.MovieID=actors_temp.MovieID AS movies_actor

求列的平均值。
query:'从电影中选择平均(rtrating)';
求列平均值。
query:'从电影中选择平均值(mcrating)';
这是我的nodejs代码。
假设app=express(),con=mysql连接。我将查询结果附加到actors变量,并通过res.send将它们发送回。

//#4 ActorID-> actor data, movies data, average rating 
app.get('/actor',function(req,res){
    let query_actorID = con.escape(req.query.actorID);
    let selectActor = 'SELECT * from actors WHERE ActorID = '+query_actorID;
        con.query(selectActor, function(err,result){
        if(err) throw err;
    console.log('retrieved actor info');
    console.log(result);
    var actor = result[0];
    var actorID = con.escape(actor.ActorID);
    //Retrieve Actor information
    let movie_query = 'SELECT * from movies LEFT JOIN (SELECT * from actedin WHERE ActorID = '+actorID+') AS actors_temp ON movies.MovieID=actors_temp.MovieID AS movies_actor'; 
    con.query(movie_query, function(err,movies){
    if(err) throw err;
        console.log('retrieved movies info');
        console.log(movies);
        actor.movies = movies;
        let RTR_query = 'SELECT AVG(RTRating) From movies_actor';
        con.query(RTR_query, function(err,average){
        if(err) throw err;
            console.log('retrieved movies info');
            console.log(average);
            actor.averageRTRating = average;
            });
        let MCR_query = 'SELECT AVG(MCRating) From movies_actor';
        con.query(RTR_query, function(err,average){
        if(err) throw err;
            console.log('retrieved movies info');
            console.log(average);
            actor.average_MCRating = average;
            });
        });

    res.send(JSON.stringify(actor));
    });
});

我的语法显然错了。我想我用错了。

w8rqjzmb

w8rqjzmb1#

我没有测试它,但它应该工作。电影的查询已经简化。您必须考虑rtr\u query和mcr\u query返回的数据是一个数组。我只做了一个查询,效果会更好。在您的代码中,实际上使用了rtr\u查询来代替mcr\u查询。我希望这会有帮助。

//#4 ActorID-> actor data, movies data, average rating 
app.get('/actor',function(req,res){
    let query_actorID = con.escape(req.query.actorID);
    let selectActor = 'SELECT * from actors WHERE ActorID = '+query_actorID;
    con.query(selectActor, function(err,result){
        if(err) throw err;

        console.log('retrieved actor info');
        console.log(result);
        if (result.length == 1) {
            let actor = result[0];
            let actorID = con.escape(actor.ActorID);
            //Retrieve Actor information
            let movie_query = 'SELECT * from actedin INNER JOIN movies ON (movies.MovieID = actedin.MovieID) WHERE actedin.ActorID = ' + actorID; 
            con.query(movie_query, function(err,movies){
                if(err) throw err;

                console.log('retrieved movies info');
                console.log(movies);
                actor.movies = movies;

                let MCR_RTR_query = 'SELECT AVG(RTRating) AS rtrating_avg, AVG(MCRating) AS mcrating_avg From movies_actor';
                con.query(MCR_RTR_query, function(err,averages){
                    if(err) throw err;

                    console.log('retrieved movies info');
                    console.log(average);
                    actor.averageRTRating = averages[0].rtrating_avg;
                    actor.average_MCRating = averages[0].mcrating_avg;
                });
            });

            res.send(JSON.stringify(actor));
        }

    });
});
eanckbw9

eanckbw92#

SELECT m.title 
  FROM actors a
  JOIN movie_actor ma
    ON ma.actor_id = a.actor_id
  JOIN movies m
    ON m.movie_id = ma.movie_id
 WHERE a.name = 'Denzel'

相关问题