cmd 清屏 cls
进入安装目录 cd ..\ mysql\bin
进入MySQL mysql -u [root] -p
输入密码
数据库 datebase
查看数据库show databases
创建数据库 create databases [database name]
删除数据库 drop database [database name]
选择数据库 use [database name]
表 table
查看表 show tables
查看表结构 describe [table name]
查看表数量
select count(table_name) from
创建表 create table [table name] (
[column name] [type](length),
......
)
删除表 drop table [table name]
插入数据
insert into [table name] ....
查询数据
查询所有数据
select * from [table name]
选择特殊行
select * from [table name] where [column name]=’[value]’...and [..] or [...]...
选择特殊列
select distinct [column name] from [table name]
模式匹配like
select * from [table name] where [column name] like ‘%[value]%’
模式字符 _
select * from [table name] where [column name] like ‘___’
正则模式 regexp ^ $ .
时间的计算
year(curdate()) 获得当前时间的年 month() dayofmonth()
月份 12月加一处理
mod(month(curdate())+1,12)
聚集函数
按种类和性别组合的动物数量:
SELECT species, sex, COUNT(*) FROM pet GROUP BY species, sex;
常用查询的例子
列的最大值 max()
select max([column name]) as max from [table name]
拥有某个列的最大值的行 max()
select * from [table name] where [column name] = (select max([column name]) from [table name])
select * form [table name] order by [column name] desc limit 1
列的最大值:按组 group by
select [column name],max(column name2) from [table name] group by [column name]
拥有某个字段的组间最大值的行
select * from [table name] t1 where t1.[column name] = (
select max(t2.[column name]) from [table name] t2 group by [column name] where t1.id = t2.id )
使用用户变量
例如,要找出价格最高或最低的物品的,其方法是:
mysql> SELECT @min_price:=MIN(price),@max_price:=MAX(price) FROM shop;
mysql> SELECT * FROM shop WHERE price=@min_price OR price=@max_price;
使用外键
根据两个键搜索
UNION
根据天计算访问量
使用AUTO_INCREMENT
参考资料: