mysql + nodejs mysql篇(2)

打響武昌第二槍發表於2019-04-10

關鍵字最好選擇大寫,可提高閱讀性,和維護更直觀 一般使用navcat視覺化化工具建立表,後增刪改查

建立鍵並進入資料庫

let create_data_segi = `CREATE DATABASE 'Segi' IF NOT EXISTS
USE segi`
複製程式碼

新建一個表,並宣告列

let create_table_test = CREATE TABLE IF NOT EXISTS `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT `文章的id`,
  `create_time` datetime NOT NULL COMMENT '建立的時間',
  `title` varchar(255) NOT NULL COMMENT '標題名稱',
  `view_number` int(11) NOT NULL DEFAULT '0' COMMENT '瀏覽次數',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COMMENT='文章表' AUTO_INCREMENT=28 ;
複製程式碼

增加資料

let insert_test = INSERT INTO `test` (`create_time`, `title`, `view_number`) values 
('2018-12-19 16:39:39', '你好世界', 5),
('2018-12-19 16:39:39', '你好世界2', 5);
複製程式碼

刪除資料

修改資料

查詢分頁

let result = {
  pageinator: {
    pageLength,
    pageNo
  }
}
let start = (pageNo - 1) * pageLength
let count = pageLength
let sql = `SELECT article.id  article_id, test.id test_id From article, test WHERE article.id = test.id AND artivle.status In(1) LIMIT ${start}, ${count}`
複製程式碼

排序

// 升序  asc 
let sql1 = `SELECT * FROM user ORDER BY user_id ASC`
// 降序 desc
let sql2 = `SELECT * FROM user ORDER BY user_id DESC`
複製程式碼

模糊查詢

let sql = `SELECT article.name, test.title FROM article,test WHERE article.id=test.id AND article.name LIKE '%${title}%' AND status IN(1) LIMIT ${start}, ${count}`
複製程式碼

分組查詢

參考連結

// 查詢各個部門最高薪資
let sql = `SELECT dept, MAX(SALARY) AS MAXIMUM FROM STAFF GROUP BY DEPT` 
//將 WHERE 子句與 GROUP BY 子句一起使用
//分組查詢可以在形成組和計算列函式之前具有消除非限定行的標準 WHERE 子句。必須在GROUP BY 子句之前指定 WHERE 子句
let sql = `SELECT DEPT, EDLEVEL, MAX( SALARY ) AS MAXIMUM
FROM staff WHERE HIREDATE > '2010-01-01'
GROUP BY DEPT, EDLEVEL
ORDER BY DEPT, EDLEVEL`
// 可以在ground by 之後使用having,and,or來限定返回分組
// 此時返回是以組為單位整組過濾
let sql = `SELECT DEPT, MAX( SALARY ) AS MAXIMUM, MIN( SALARY ) AS MINIMUM
FROM staff
GROUP BY DEPT
HAVING COUNT( * ) >2
ORDER BY DEPT`
複製程式碼

左聯LEFT JOIN與 WHERE

左聯的理解是,在沒有匹配到值的時候也會放回null where是過濾的意思,在沒有匹配到值時會過濾掉 看如下程式碼

let create_table_user = `
CREATE TABLE IF NOT EXISTS 'user'(
'id' int(11) NOT NULL AUTO_INCREMENT COMMENT '使用者的id',
'name' varchar(12)  NOT NULL COMMENT '使用者名稱',
‘score’ varchar (3) NOT NULL COMMENT '分數',
PRMARY KEY ('id)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COMMENT='使用者表 AUTO_INCREMENT=28 ;
`
let create_table_city =   `
CREATE TABLE IF NOT EXISTS 'city'(
'id' int(11) NOT NULL AUTO_INCREMENT COMMENT '城市記錄id',
'user_id' int(11)  NOT NULL COMMENT '使用者id',
‘city’ varchar (3) COMMENT '使用者城市',
PRMARY KEY ('id)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COMMENT='使用者城市表' AUTO_INCREMENT=28 ;
`
let insert_user = INSERT INTO user ('id','name','score') VALUES (1,'張三',89), (2,'李四',88), (3,'王五',99),(4,'趙六',99);
let insert_city = INSERT INTO city ('id','user_id','city') VALUES (1,2,'湖南’), (2,2,'上海'), (3,1,null);

left join 查詢(此時趙6也會返回,city為null)
let select_leftJoin = `SELECT id,name,city from user LEFT JOIN city on(user.id = city.user_id)`

where 查詢 (此時趙6 不會會返回)
let select_where= `SELECT id,name,city from user ,city WHERE user.id=city.user_id`

複製程式碼

Mysql中natural join和inner join的區別

nner join---- SELECT * FROM TableA INNER JOIN TableB USING (Column1) SELECT * FROM TableA INNER JOIN TableB ON TableA.Column1 = TableB.Column1 natural join--它會去除重複的列名 SELECT * FROM TableA NATURAL JOIN Table

函式

  1. count(*) / count(1) 總數在分頁查詢用的較多
  2. 去重distinct(name)
  3. IFNULL() 沒有返回空,如下取第二高成績人名 select IFNULL((select distinct(Salary) from Employee order by Salary desc limit 1,1),null) as SecondHighestSalary
  4. sum( SALARY ) 求某一列的總和
  5. MAX( SALARY ) 最大值
  6. MIN( SALARY ) 最小值

相關文章