MySQL 基本语句记录
2016-05-06 11:23
567 查看
-- 查询
select column1,column2 from table
select column1,column2 from table where id = 1
-- 查询:去掉某字段重复项,结果为这个字段
SELECT DISTINCT (column) FROM table
-- 查询:[b]去掉某字段重复项,结果可指定字段[/b]
select * from tablegroup by column
-- 内连接查询
select * from table1,table2 where table1.id = table2.id
select * from table1 inner join table2 on table1.id = table2.id
-- 左右连接查询
select * from table1 right join table2 on table1.id = table2.id
select * from table1 left join table2 on table1.id = table2.id
-- 全连接查询
select * from table1 right join table2 on table1.id = table2.id
union
select * from table1 left join table2 on table1.id = table2.id
-- 限制查询
select * from table1 limit 0,5
------------------------------------------------------------------------------------------------------------
-- 插入
insert into table (column1,column2) values (column_value1,column_value2)
------------------------------------------------------------------------------------------------------------
-- 更新
update table set column1 = 'column_value1',column2 = 'column_value2' where id = 1
------------------------------------------------------------------------------------------------------------
-- 删除
delete from table where id = 1
------------------------------------------------------------------------------------------------------------
-- 排序
select * from table1 order by id
select * from table2 order by id desc
------------------------------------------------------------------------------------------------------------
-- 统计
select max(id) from table1
select min(id) from table1
select sum(id) from table1
select avg(id) from table1
select count(id) from table1 where pid = 1
select count(id) from table1 where table1.pid = table2.pid group by table1.pid
------------------------------------------------------------------------------------------------------------
-- 使用select结果语句创建新表
CREATE TABLE new_table (SELECT * FROM old_table);
CREATE TABLE box_total(
select pathway,pathwayName
FROM (
select pathway,pathwayName from box_human
UNION
select pathway,pathwayName from box_monkey
UNION
SELECT pathway,pathwayName from box_mouse
UNION
SELECT pathway,pathwayName from box_ox
UNION
SELECT pathway,pathwayName from box_pig
UNION
SELECT pathway,pathwayName from box_rat
order by pathway
) as TB
)
select column1,column2 from table
select column1,column2 from table where id = 1
-- 查询:去掉某字段重复项,结果为这个字段
SELECT DISTINCT (column) FROM table
-- 查询:[b]去掉某字段重复项,结果可指定字段[/b]
select * from tablegroup by column
-- 内连接查询
select * from table1,table2 where table1.id = table2.id
select * from table1 inner join table2 on table1.id = table2.id
-- 左右连接查询
select * from table1 right join table2 on table1.id = table2.id
select * from table1 left join table2 on table1.id = table2.id
-- 全连接查询
select * from table1 right join table2 on table1.id = table2.id
union
select * from table1 left join table2 on table1.id = table2.id
-- 限制查询
select * from table1 limit 0,5
------------------------------------------------------------------------------------------------------------
-- 插入
insert into table (column1,column2) values (column_value1,column_value2)
------------------------------------------------------------------------------------------------------------
-- 更新
update table set column1 = 'column_value1',column2 = 'column_value2' where id = 1
------------------------------------------------------------------------------------------------------------
-- 删除
delete from table where id = 1
------------------------------------------------------------------------------------------------------------
-- 排序
select * from table1 order by id
select * from table2 order by id desc
------------------------------------------------------------------------------------------------------------
-- 统计
select max(id) from table1
select min(id) from table1
select sum(id) from table1
select avg(id) from table1
select count(id) from table1 where pid = 1
select count(id) from table1 where table1.pid = table2.pid group by table1.pid
------------------------------------------------------------------------------------------------------------
-- 使用select结果语句创建新表
CREATE TABLE new_table (SELECT * FROM old_table);
CREATE TABLE box_total(
select pathway,pathwayName
FROM (
select pathway,pathwayName from box_human
UNION
select pathway,pathwayName from box_monkey
UNION
SELECT pathway,pathwayName from box_mouse
UNION
SELECT pathway,pathwayName from box_ox
UNION
SELECT pathway,pathwayName from box_pig
UNION
SELECT pathway,pathwayName from box_rat
order by pathway
) as TB
)
相关文章推荐
- MySQL中的integer 数据类型
- MySQL存储过程
- mysql中int、bigint、smallint 和 tinyint的区别与长度
- mysql load data 导出、导入 csv
- source命令执行SQL脚本文件
- MySQL创建用户及权限控制
- MySQL管理数据表
- linux下mysql添加用户
- mysql procedure
- mysql触发器
- MySQL 备份和恢复策略
- mac下安装mysql(转载)
- mysql 修改编码 Linux/Mac/Unix/通用(杜绝修改后无法启动的情况!)
- MySQL数据的导出、导入(mysql内部命令:mysqldump、mysql)
- mysql数据行转列
- Linux下修改MySQL编码的方法
- MySQL Server 日志
- MySQL 安全事宜
- MySQL 备份与恢复