node操作數據庫基本語法

//引入mysql
const mysql = require("mysql");

//設置連接信息
let options = {
    host:"localhost",
    user:"root",
    password:"123456",
    database:"node"
}
//建立連接對象
let con = mysql.createConnection(options);

//建立連接
con.connect((err)=>{
    if(err){
        console.log(err);
    }else{
        console.log("連接成功");
    }
});

//查詢語句
let selectUser = "SELECT * FROM USER";

//執行查詢語句
con.query(selectUser,(err,results,fields)=>{//err:報錯信息,res:當前查詢表的數據信息,fileds:表字段
    if(err){
        console.log(err);
    }else{
        console.log(results);
        console.log(fields);
    }
});

//新建agwenbi表
let creatTable = 'CREATE TABLE `node`.`agwenbi`  ( `id` int(0) NOT NULL AUTO_INCREMENT, `username` varchar(255) NOT NULL, `password` varchar(255) NOT NULL,`email` varchar(255) NULL,PRIMARY KEY (`id`));';
con.query(creatTable,(err,results)=>{
    console.log(err);
    console.log(results);
});

//插入數據
let insertData = "insert into agwenbi (`id`,`username`,`password`,`email`) values (1,'Agwenbi','wenbi123456','[email protected]')";
con.query(insertData,(err,results)=>{
    console.log(err);
});

//插入動態數據
let insertData1 = "insert into agwenbi (`username`,`password`,`email`) values (?,?,?)";//設置?提供佔位符
con.query(insertData1,["agwenbi","[email protected]","[email protected]"],(err,results)=>{
    console.log(err);
});
let search = "select * from lol where id <= 700";
let search = "select * from lol where id <= 800 and id >= 700" //查找id大於等於700小於等於800的數據
let search = "select * from lol where id between 700 and 800";//查找id在700至800之間的數據
let search = "select * from lol where id = 700 or id = 800";//查找id等於700或id等於800的數據
let search = "select * from lol where id in(700,800)";//查找id等於700或id等於800的數據
let search = "select * from lol where username like '%虛空%'";//查詢名字中帶有虛空的數據
let search = "select * from lol where username like '虛空______'";//查找名字以虛空開頭的並且後面是六個字的數據,總共七個字符
let search = "select * from lol where info is NULL";//查找信息字段是NULL的數據
let search = "select * from lol where info is not NULL";//查找信息字段不是NULL的數據
//連表查詢操作
let search = "select * from book inner join author on book.authorId = author.authorId";
//查詢book表與author表,並且book表的authorId字段值等於author表的authorId字段值的數據

let search = "select * from book inner join author on book.authorId = author.authorId where author.author = '天下霸唱'";
//查詢book表與author表,並且book表的authorId字段值等於author表的authorId字段值,並且author的author字段值等於天下霸唱的數據

let search = "select * from book as a inner join author as b on a.authorId = b.authorId";
//查詢book表重命名爲a與author表重命名爲b,並且book表的authorId字段值等於author表的authorId字段值的數據
//多級連表查詢操作
let search = "select book.id,book.book,author.author,channel.channelName from book inner join author on book.authorId = author.authorId inner join channel on book.channelId = channel.channelId";
//先查詢book與author的表數據,再將查詢出來的數據與channel表連表查詢,得出結果,結果中,只顯示book表的的id字段、book字段列,author表的author列,channel表的channelName列數據
//自關聯查詢
select c1.Id,c1.name as '省份',c2.Name as '市區' from china as c1 inner join china as c2 on c1.Id = c2.Pid where c1.Name = '廣東省';
//查詢廣東省的所有市
//子查詢

select * from student where age in (select age from student where age < 19);
//首先查詢student表age字段小於19的數據,然後將查詢的結果作爲條件查詢


select * from student where exists (select age from student where age >= 30);
//首先查詢student表age字段大於等於30的數據,如果有就繼續查詢,沒有就放棄查詢
-- 事務
BEGIN;
insert into book (`book`,`content`) values ('小時代4','小時代4內容');

insert into channel (`channelName`) value ('土豆');

COMMIT;

-- 只有當兩條插入語句都成功的時候,數據纔會插入到數據庫中

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章