数据库sql操作基础和ORM
show databases;-- `显示所有的库`use koa2weibo;-- `启用当前的库`select * from blog order by id desc;//通过id 倒序查询select * from users; //查询users表 所有数据 一般不用 而用下面条件查询select username,`password` from users where username
·
一,数据库基本操作,连表查询
show databases;
-- `显示所有的库`
use koa2weibo;
-- `启用当前的库`
select * from blog order by id desc; //通过id 倒序查询
select count(*) from blog; //查询总数
select count(*) as `count` from blog; //查询总数用一个别名
select count(id) as `count` from blog;//查询总数不需要查所有的 查id就行了
select * from blog order by id desc limit 2 offset 2; //查表进行分页每页limit2条
select * from users; //查询users表 所有数据 一般不用 而用下面条件查询
select username,`password` from users where username='aaa' and `password`='123'; //条件查询
-- select username,password from users //条件查询
insert into users (username,`password`,nickname) values('aaa','123','二个eee猴子'); //新增
insert into blog (title,content,userid) values('标题4','内容4',2); //新增
update blog set content='内容sssss' where id ='1'; //更新
delete from blog where id = '3' //删除
-- '连表查询'
select * from blog inner join users on users.id = blog.userid;
select blog.*,users.username,users.nickname from blog inner join users on users.id = blog.userid;
二,ORM知识点
直接在node里面写sql语句查询比较底层,也不太方便。用ORM(对象关系映射)工具简化操作。
我们用sequelize
npm i mysql2 sequelize -D

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐
所有评论(0)