javascript - 如何将mysql查询转换为sequelize orm

标签 javascript mysql left-join sequelize.js

我需要使用sequelize执行多个LEFT JOIN查询:

SELECT movie, genre FROM `yt_movies` M 
LEFT JOIN `genres_link` GL ON M.id = GL.movie_id
LEFT JOIN `genres` G ON GL.genre_id = G.id
WHERE M.id = 1098

我试过了

const YtMovies = db.yt_movies;
const Genres = db.genres;
const GenresLink = db.genres_link;

YtMovies.hasMany(GenresLink, { as: 'GL', foreignKey: 'movie_id' });
YtMovies.hasMany(Genres, { as: 'G', foreignKey: 'genre' });

const res = await db.yt_movies.findAll({
                    attributes: ['movie'],
                    where: { id: movie_id },
                    include: [
                        {
                            model: db.genres_link,
                            as: 'GL',
                            required: false,
                            attributes: ['genre_id'],
                        },
                        {
                            model: db.genres,
                            required: false,
                            as: 'G',
                            attributes: ['genre'],
                        },
                    ],
                });

返回的查询看起来像

SELECT
`yt_movies`.`id`,
`yt_movies`.`movie`,
`GL`.`id` AS `GL.id`,
`GL`.`genre_id` AS `GL.genre_id`,
`G`.`id` AS `G.id`,
`G`.`genre` AS `G.genre`
FROM `yt_movies` AS `yt_movies`
LEFT OUTER JOIN `genres_link` AS `GL` ON `yt_movies`.`id` = `GL`.`movie_id`
LEFT OUTER JOIN `genres` AS `G` ON `yt_movies`.`id` = `G`.`genre`
WHERE `yt_movies`.`id` = 1098;

在最后两个字符串中,我们可以看到它使用 ON 和 yt_movies 但我希望它能与 genres_link

一起使用 ON
LEFT JOIN `genres` AS G ON GL.genre_id = G.id <-- expected

我想我不太了解表关联并且搞乱了 hasMany 或者我错过了另一个语句

表格看起来像

yt_movies

| id | movie   | pj | pq|
|----|---------|----|---|
|  1 |Avatar   |    |   |
|  2 |Predator |    |   |
|  3 |...      |    |   | 

流派链接

| id | genre_id| movie_id |
|----|---------|----------|
|  1 |  12     |    1     | // avatar
|  2 |  13     |    2     | // predator
|  3 |  14     |    2     | // predator

流派

| id | genre    |
|----|----------|
| 12 | action   | 
| 13 | thriller | 
| 14 | horror   | 

我所做的就是设法执行第一个 LEFT JOIN...添加第二个 include 没有帮助,而且恐怕即使在阅读文档之后我也没有真正理解表关联:|

我想我需要使用belongsToMany,但目前我不明白如何:))

我感谢所有的帮助! 谢谢,HALP!

最佳答案

要加入 A->B->C,您应该将 C 的包含内容嵌套在 B 的包含内容内,例如

A.findAll({    
    include: [
       {
         model: B,
         include: [
             {model: C}
             ]
         }
     ]
     })

但是,如果表genreations_link除了电影和流派的PK之外没有其他属性,则使用through

   YtMovies.belongsToMany(Genres, {through: GenresLink, foreignKey: 'movie_id' });
   Genres.belongsToMany (YtMovies,{through: GenresLink, foreignKey: 'genre_id '});

    YtMovies.findAll({    
       include: [
          {
           model: Genres, 
           required : true,
           through: GenresLink 
          }
          ]
       });

manual有一些关于这个主题的有用信息...

关于javascript - 如何将mysql查询转换为sequelize orm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55354324/

相关文章:

java - MySQL 和 JSP 数据库与 Glassfish 服务器 4 的连接

mysql - 计算特定行之后的行数

mysql - SQL DELETE 从多个表

javascript - Hash(#) 在 URL 中的使用

javascript - React JS 自定义侧边栏导航

Javascript 到 Mongodb 查询直通。安全的?不好的做法?

javascript - Tabletop.js 看不到 Google 电子表格数据

php - SQL在查询中添加子索引列?

php - 如何生成唯一的仅整数 ID,如 Facebook Twitter

mysql - 'posts' 和 'postmeta' WordPress 表的嵌套连接查询