顺序连接表

时间:2018-09-24 17:14:45

标签: join sequelize.js query-builder

我正试图通过加入4个表从db中获取数据。由于续集对我来说是新的,所以我无法帮助任何人。这是必须在ORM序列中进行的查询。

import java.util.*;

public class path {


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t  =1;
        int n;
        int values[][];

        while(t-->0){

            n = 17;
            values = new int [n][n];

            int input[]={67,280,171,381,930,781,925,4,393,380,246,433,762,258,5,166,315,503,385,728,854,350,464,288,304,80,689,56,313,843,92,379,122,614,111,403,394,387,406,138,767,651,571,880,260,927,398,926,429,782,653,634,132,468,274,435,548,314,490,212,156,933,942,629,546,404,31,292,142,436,781,260,86,703,140,697,630,537,622,410,318,275,44,801,94,669,236,993,982,77,204,137,10,497,765,907,900,147,550,42,582,331,301,19,33,792,715,14,680,336,424,350,962,467,150,408,135,737,400,468,814,956,956,175,452,72,433,704,218,983,97,799,665,749,169,49,541,883,63,572,570,486,921,884,304,423,291,790,159,42,257,324,997,212,498,801,283,283,504,500,617,952,650,281,700,818,329,592,52,743,164,621,228,436,856,883,858,498,672,17,540,928,340,536,139,190,336,773,472,191,272,88,142,921,720,842,90,400,433,141,143,948,114,722,384,969,605,593,819,276,961,358,556,301,893,46,842,581,819,665,771,90,104,265,363,823,106,452,574,890,945,68,190,58,790,925,378,746,517,196,373,478,905,280,130,798,326,323,730,144,987,500,585,90,764,947,264,221,751,837,463,47,257,652,456,46,576,185,143,444,381,867,921,285,147,402,434,472,724,163,615,710,15,551,151,130,498,414,703};
            int k=0;
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                    values[i][j] = input[k];
                    k++;
                }
            }

            for ( int r = n-2 ; r >=0 ; r-- ) {

                for ( int c = 0 ; c < n ; c++ ) {

                    if (c == 0) {
                        values[r][c] += Math.max(values[r+1][c], values[r+1][c+1]);

                    } else if (c == n-1) {
                        values[r][c] += Math.max(values[r+1][c], values[r+1][c-1]);
                    } else {
                        values[r][c] += Math.max(values[r+1][c], Math.max(values[r+1][c-1], values[r+1][c+1]));
                    }
                }
            }

            int max = values[0][0];

            for(int c = 1; c <= n-1 ; c++) 
                max = Math.max(max, values[0][c]);

            System.out.println(max);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您需要首先定义Model,然后使用它来查询模型的Instance(或Instance的数组)。列名是从模型定义中推断出来的,也可以使用attributes来指定。使用include来联接其他表。

模型

/* Define `Models` for Page, PageType, Exam, and Menu and 
   create associations/relationships between them */

const PageType = sequelize.define(
  'page_type',
  { /* define fields */ },
  { /* define options */ }
);
module.exports = PageType;

const Exam = sequelize.define(
  'exam',
  { /* define fields */ },
  { /* define options */ }
);
module.exports = Exam;

const Menu = sequelize.define(
  'menu',
  { /* define fields */ },
  { /* define options */ }
);
module.exports = Menu;

// ...add additional Models

const Page = sequelize.define(
  'page',
  { /* define fields */ },
  { /* define options */ }
);
// add relationships to other models
Page.associate = (models) => {
  Page.belongsTo(models.PageType);
  Page.hasMany(models.Exam);
  Page.hasMany(models.Menu);
  // ...additional relationships
};
module.exports = Page;

查询

// Query using the following syntax
const pages = await Page.findAll({
  attributes : ['page_id', 'page_title', 'is_published', 'created_date', 'status_id'],
  include: [
    {
      model: PageType,
      attributes : ['page_type_name'],
    },
    {
      model: Exam,
      attributes : ['exam_id', 'exam_name'],
    },
    {
      model: Menu,
      attributes : ['name'],
    },
  ],
});
相关问题