nodejs中mysql的常⽤语句
命令语义
select提取数据
update更新数据
delete删除数据
insert into插⼊数据
create database创建数据库
alter database修改数据库简单的mysql语句
create table创建表
alter table修改表
drop table删除表
create index创建索引(搜索键)
drop index删除索引注:命令可以⼩写,为⽅便区分,⽂中所有命令使⽤⼤写
select
语法:SELECT column_name,column_name FROM table_name.
例⼦:SELECT * FROM user WHERE id=1;
关键字distinct:⽤于返回唯⼀不同的值
SELECT DISTINCT class FROM student
⼀个列中可能会包含多个重复的值,这⾥仅仅列出不同(distinct)的值
select 语句后⾯需要跟⼦句来进⾏筛选或者操作数据,⼦句在下⽂有详细介绍:
1. where
SELECT column_name,column_name FROM table_name WHERE column_name operator value;
例:SELECT * FROM student WHERE age=15.
and & or 运算符
SELECT * FROM user WHERE age>15 AND (conutry=‘china’ OR country=‘usa’)
2. order
对搜索结果按照⼀列或者多列进⾏排序。
语法:SELECT column_name,column_name FROM table_name ORDER BY column_name,column_name.排序关键字:ASC|DESC (升序|降序)。
例⼦1:SELECT * FROM user ORDER BY age。
例⼦2:SELECT * FROM user ORDER BY age DESC;(按照年龄降序排序)
例⼦3:SELECT * FROM user ORDER BY age ASC, height DESC;(按照年龄升序排列,年龄相同按照⾝⾼降序排列)
3. group
聚类:合并相同
例⼦:SELECT * FROM students GROUP BY class;(统计每班⼈数)。
GROUP BY class, 以class为合并依据,单独⽤只是个去重的作⽤
SELECT class FROM student GROUP BY class;
只是单独列出有哪些班级
SELECT class,COUNT(class) FROM student GROUP BY class
每个班的平均分
SELECT class,AVG(score) FROM student GROUP BY class;
SELECT class,AVG(score),MAX(score),MIN(SCORE),AVG(age) FROM student GROUP BY class.
4. limit
SELECT * form 表名 WHERE 条件 limit 0,10; //从第0条开始,要10条数据
SELECT * form 表名 WHERE 条件 limit 5,-1; //检索6到最后⼀条数据
SELECT * form 表名 WHERE 条件 limit 5; //检索前5条数据
insert into
语法1:INSERT INTO table_name (column_name,column_name,…) VALUES (column_name,column_name,…)
语法2:INSERT INTO table_name VALUES (column_name,column_name,…)
例⼦:INSERT INTO Websites (name, url, country)
VALUES (‘stackoverflow’, ‘’, ‘IND’);
update
语法:UPDATE table_name SET column1=value1,column2=value2,… WHERE some_column=some_value.
例⼦:UPDATE user SET name=‘alex’ WHERE id=1.
注:where字句为判断条件,如果省略,则所有数据都将被更新!
delete
删除表中的⾏
语法: DELETE FROM table_name WHERE column_name=value;
例⼦:DELETE FROM user WHERE name=‘alex’ AND age=15;
可以在不删除表的情况下,删除表中所有⾏。这意味着表的结构、属性、索引将保持不变:DELETE * FROM table_name 或者 DELETE FROM table_name.
本⽂参考,作者adoctors 更多sql语句和技巧可参见

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。