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","abc@123456","[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;

-- 只有当两条插入语句都成功的时候,数据才会插入到数据库中

 

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