1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| const alasql = require('alasql');
alasql(`CREATE TABLE users (age number, name string, money number, company number)`); alasql(`CREATE TABLE companies (id number, name string)`); for (let i = 0; i < 10; i++) { const number = Math.ceil(Math.random() * 10); const sql = `INSERT INTO users VALUES (${i}, '张${i}', ${number}, ${number})`; alasql(sql); alasql(`INSERT INTO companies VALUES (${number}, '公司' + ${number})`); }
const mysql = alasql(` SELECT u.age, u.name, u.money, c.name company_name FROM users u INNER JOIN companies c ON u.company=c.id `); console.log(mysql);
[ { age: 0, name: '张0', money: 1, company_name: '公司1' }, { age: 1, name: '张1', money: 2, company_name: '公司2' }, { age: 1, name: '张1', money: 2, company_name: '公司2' }, { age: 1, name: '张1', money: 2, company_name: '公司2' }, { age: 1, name: '张1', money: 2, company_name: '公司2' }, { age: 2, name: '张2', money: 7, company_name: '公司7' }, { age: 2, name: '张2', money: 7, company_name: '公司7' }, { age: 3, name: '张3', money: 4, company_name: '公司4' }, { age: 3, name: '张3', money: 4, company_name: '公司4' }, { age: 3, name: '张3', money: 4, company_name: '公司4' }, { age: 4, name: '张4', money: 7, company_name: '公司7' }, { age: 4, name: '张4', money: 7, company_name: '公司7' }, { age: 5, name: '张5', money: 2, company_name: '公司2' }, { age: 5, name: '张5', money: 2, company_name: '公司2' }, { age: 5, name: '张5', money: 2, company_name: '公司2' }, { age: 5, name: '张5', money: 2, company_name: '公司2' }, { age: 6, name: '张6', money: 4, company_name: '公司4' }, { age: 6, name: '张6', money: 4, company_name: '公司4' }, { age: 6, name: '张6', money: 4, company_name: '公司4' }, { age: 7, name: '张7', money: 4, company_name: '公司4' }, { age: 7, name: '张7', money: 4, company_name: '公司4' }, { age: 7, name: '张7', money: 4, company_name: '公司4' }, { age: 8, name: '张8', money: 2, company_name: '公司2' }, { age: 8, name: '张8', money: 2, company_name: '公司2' }, { age: 8, name: '张8', money: 2, company_name: '公司2' }, { age: 8, name: '张8', money: 2, company_name: '公司2' }, { age: 9, name: '张9', money: 2, company_name: '公司2' }, { age: 9, name: '张9', money: 2, company_name: '公司2' }, { age: 9, name: '张9', money: 2, company_name: '公司2' }, { age: 9, name: '张9', money: 2, company_name: '公司2' } ]
|