数据库语句编写相关的:
MySQL 数据类型
MySQL中定义数据字段的类型对你数据库的优化是非常重要的。
MySQL支持多种类型,大致可以分为三类:数值、日期/时间和字符串(字符)类型。
数值类型:
提供一个链接:
http://www.w3school.com.cn/sql/sql_datatypes.asp
数据库约束:
http://www.runoob.com/sql/sql-constraints.html
查询类型:
查询关键字:
普通查询:
select all stu_id ,stu_name,stu_age ,stu_address
from [cjda].[dbo].[t_student]
where stu_id=2
and stu_age =10
or stu_address='shanghai'
and stu_address is not null
分组查询:
关键字: group by having
应用:
分组去重:
delete
from [cjda].[dbo].[t_student]
where stu_id
not in (
select MIN(stu_id)
from [cjda].[dbo].[t_student]
group by stu_name,[stu_age],[stu_address],[stu_teach_num],[class_id])
分组内查询
rank() over (order by 排序字段 顺序)
rank() over (partition by 分组字段 order by 排序字段 顺序)
分页查询:
sql server 和mysql不一样的地方3
select top (n-m+1) id from tablename
where id not in (
select top m-1 id from tablename
)
mysql:
select * from tablename limit n,m
子查询:
总结一下:
查出来为一个值:
当查出来为一个值得时候可以直接拿过来用
select [stu_name]
,[stu_age]
,[stu_address]
,[stu_teach_num]
from [cjda].[dbo].[t_student]
where stu_teach_num >
(select
AVG(stu_teach_num)
from [cjda].[dbo].[t_student] /*查询出来是一个值**/
as avg)
查询出来是一个集合的那种:
查询出来是集合的那种必须用相应的关键词,例如:
in,not in,any,all等 在使用in或者not in的时候我们最好使用固定的数据例如 in(3,5)
any(1,2)
select *
from [cjda].[dbo].[t_student]
where stu_id = any(
select MIN(stu_id)
from [cjda].[dbo].[t_student]
group by stu_name,stu_age,stu_address,stu_teach_num,class_id /*查询出来为一个集合**/
)
查询出来为一行的那种:
查询出来的数据当做表来查询:
select t.stu_id from (select stu_id,stu_age,stu_address from [cjda].[dbo].[t_student]
)as t
表连接:
内连接:查询出左表和右表都有的数据
左连接:查询出符合坐标中有的数据,有表中没有的设置为空
右连接:查询出右表中有的数据,左表中没有的设置为空
全外连接:查询出全部的数据,没有的数据设置为空
/****** Script for SelectTopNRows command from SSMS ******/
SELECT TOP 1000 c.[class_id]
,[class_name]
,[class_stu_num]
,[class_teach_num] ,
s.*
FROM [cjda].[dbo].[t_class] as c
left join [cjda].[dbo].[t_student] as s
on c.class_id = s.class_id
/****** Script for SelectTopNRows command from SSMS ******/
SELECT TOP 1000 c.[class_id]
,[class_name]
,[class_stu_num]
,[class_teach_num] ,
s.*
FROM [cjda].[dbo].[t_class] as c
right join [cjda].[dbo].[t_student] as s
on c.class_id = s.class_id
/****** Script for SelectTopNRows command from SSMS ******/
SELECT TOP 1000 c.[class_id]
,[class_name]
,[class_stu_num]
,[class_teach_num] ,
s.*
FROM [cjda].[dbo].[t_class] as c
inner join [cjda].[dbo].[t_student] as s
on c.class_id = s.class_id
/****** Script for SelectTopNRows command from SSMS ******/
SELECT TOP 1000 c.[class_id]
,[class_name]
,[class_stu_num]
,[class_teach_num] ,
s.*
FROM [cjda].[dbo].[t_class] as c
full outer join [cjda].[dbo].[t_student] as s
on c.class_id = s.class_id