Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

javascript - Node.js Sequelize join two table and create a third table with the data created and count the sum of the same number of the first column

I am trying to use Sequelize and my brain must have been burnt from the whole project and now I cannot even join tables lol so here we go:
the task is I have two model that creates two tables and another model empty (so an empty table) I would like to join the two table in the third one and after in another statement I would like to count how many users(example) has the first column if there are two user with the same name means that is only one so will count just as one.

module.exports = (sequelize, Sequelize) => {
        const Gig = sequelize.define("gig", {
              a: {
                type: Sequelize.STRING,
              },
              b: {
                type: Sequelize.STRING,
              },
              c: {
                type: Sequelize.STRING,
              }
        });
        
      
        return Gig;
      };
module.exports = (sequelize, Sequelize) => {
        const Gig1 = sequelize.define("gig1", {
              a: {
                type: Sequelize.STRING,
              },
              b: {
                type: Sequelize.STRING,
              },
              c: {
                type: Sequelize.STRING,
              }
        });
        
      
        return Gig1;
      };
module.exports = (sequelize, Sequelize) => {
        const Gig2 = sequelize.define("gig2", {
              a: {
                type: Sequelize.STRING,
              },
              b: {
                type: Sequelize.STRING,
              },
              c: {
                type: Sequelize.STRING,
              }
        });
        
      
        return Gig2;
      };

this is in another file

router.get("/", (req, res) => {
        db.Gig.findAll({here the magic should happen thanks guys})
          .then((users) => {
            console.log(users);
            res.sendStatus(200);
          })
          .catch((err) => {
            console.log(err);
          });
      });

so Gig+Gig1 = Gig2 then count the user in a (so give me the sum without clones)

I found this article written below that is similar to what I am looking for but adding the result to Gig2 that is empty and then do the calcs. *

UNION ALL and an exclusion join
One way to make UNION include only the duplicates I want is to use an exclusion join to eliminate anything from the second result that is already included in the first, like this:
select * from apples as a
   left outer join oranges as o on a.price = o.price
union all
select * from apples as a
   right outer join oranges as o on a.price = o.price
where a.price is null;
This handles duplicate rows correctly and doesn’t include anything it shouldn’t. It’s necessary to use UNION ALL instead of plain UNION, which would eliminate the duplicates I want to keep. This may be significantly more efficient on large result sets, since there’s no need to sort and remove duplicates.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...