温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

常用的SQL语句

发布时间:2020-07-10 12:40:11 来源:网络 阅读:236 作者:xiaoxmok 栏目:关系型数据库

SELECT 

sql="select * from  数据表  where  字段名=字段值  order by  字段名  [desc]" 

 

sql="select * from  数据表  where  字段名  like %字段值% order by  字段名 

[desc]" 

 

sql="select top 10 * from  数据表  where  字段名  order by  字段名  [desc]" 

 

sql="select * from  数据表  where  字段名  in ( 1, 2, 3)" 

 

sql="select * from  数据表  where  字段名  between   1 and   2" 



select *(列名) from table_name(表名) where column_name operator value 

ex:(宿主

select * from stock_information where stockid = str(nid) 

stockname = 'str_name' 

stockname like '% find this %' 

stockname like '[a-zA-Z]%' --------- ([]指定值的范围

stockname like '[^F-M]%' --------- (^排除指定范围

---------  只能在使用 like 关键字的 where 子句中使用通配符

or stockpath = 'stock_path' 

or stocknumber < 1000 

and stockindex = 24 

not stock*** = 'man' 

stocknumber between 20 and 100 

stocknumber in(10,20,30) 

 

order by stockid desc(asc) ---------  排序,desc-降序,asc-升序 

order by 1,2 --------- by 列号 

stockname = (select stockname from stock_information where stockid = 4) 

---------  子查询 

---------  除非能确保内层 select 只返回一个行的值, 

---------  否则应在外层 where 子句中用一个 in 限定符 

select distinct column_name form table_name --------- distinct 指定检索独有的列值, 

不重复 

select stocknumber ,"stocknumber + 10" = stocknumber + 10 from table_name 

select stockname , "stocknumber" = count(*) from table_name group by stockname 

--------- group by  将表按行分组,指定列中有相同的值 

having count(*) = 2 --------- having 选定指定的组 

select * from table1, table2 where table1.id *= table2.id --------  左外部连接,table1 中有的而 table2 中没有得 

 null 表示 

table1.id =* table2.id --------  右外部连接 

select stockname from table1 

union [all] ----- union 合并查询结果集,all-保留重复行 

select stockname from table2 

insert 

sql="insert into  数据表  (字段 1,字段 2,字段 3 ) valuess ( 1, 2, 3 )" 

 

sql="insert into  目标数据表  select * from 源数据表" (把源数据表的记录添加到 

目标数据表

 

insert into table_name (Stock_name,Stock_number) value ("xxx","xxxx") 

value (select Stockname , Stocknumber from Stock_table2)---value  select 语句 

update 

sql="update  数据表  set  字段名=字段值  where  条件表达式

 

sql="update  数据表  set  字段 1= 1,字段 2= 2   字段 n= n where  条件表 

达式

 

 

update table_name set Stockname = "xxx" [where Stockid = 3] 

Stockname = default 

http://hi.baidu.com/ttcc2009 


 

Stockname = null 

Stocknumber = Stockname + 4 

delete 

sql="delete from  数据表  where  条件表达式

 

sql="delete from  数据表" (将数据表所有记录删除

 

delete from table_name where Stockid = 3 

truncate table_name -----------  删除表中所有行,仍保持表的完整性 

drop table table_name ---------------  完全删除表 



select *(列名) from table_name(表名) where column_name operator value 

ex:(宿主

select * from stock_information where stockid = str(nid) 

stockname = 'str_name' 

stockname like '% find this %' 

stockname like '[a-zA-Z]%' --------- ([]指定值的范围

stockname like '[^F-M]%' --------- (^排除指定范围

---------  只能在使用 like 关键字的 where 子句中使用通配符

or stockpath = 'stock_path' 

or stocknumber < 1000 

and stockindex = 24 

not stock*** = 'man' 

stocknumber between 20 and 100 

stocknumber in(10,20,30) 

 

order by stockid desc(asc) ---------  排序,desc-降序,asc-升序 

order by 1,2 --------- by 列号 

stockname = (select stockname from stock_information where stockid = 4) 

---------  子查询 

---------  除非能确保内层 select 只返回一个行的值, 

---------  否则应在外层 where 子句中用一个 in 限定符 

select distinct column_name form table_name --------- distinct 指定检索独有的列值, 

不重复 

select stocknumber ,"stocknumber + 10" = stocknumber + 10 from table_name 

select stockname , "stocknumber" = count(*) from table_name group by stockname 

--------- group by  将表按行分组,指定列中有相同的值 

having count(*) = 2 --------- having 选定指定的组 

select * from table1, table2 where table1.id *= table2.id --------  左外部连接,table1 中有的而 table2 中没有得 

 null 表示 

table1.id =* table2.id --------  右外部连接 

select stockname from table1 

union [all] ----- union 合并查询结果集,all-保留重复行 

select stockname from table2 

insert 

sql="insert into  数据表  (字段 1,字段 2,字段 3 ) valuess ( 1, 2, 3 )" 

 

sql="insert into  目标数据表  select * from 源数据表" (把源数据表的记录添加到 

目标数据表

 

insert into table_name (Stock_name,Stock_number) value ("xxx","xxxx") 

value (select Stockname , Stocknumber from Stock_table2)---value  select 语句 

update 

sql="update  数据表  set  字段名=字段值  where  条件表达式

 

sql="update  数据表  set  字段 1= 1,字段 2= 2   字段 n= n where  条件表 

达式

 

 

update table_name set Stockname = "xxx" [where Stockid = 3] 

Stockname = default 

http://hi.baidu.com/ttcc2009 


 

Stockname = null 

Stocknumber = Stockname + 4 



IN 操作符

IN 操作符允许我们在 WHERE 子句中规定多个值。

SQL IN 语法

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...)




AND  OR 运算符

AND  OR 可在 WHERE 子语句中把两个或多个条件结合起来。

如果第一个条件和第二个条件都成立,则 AND 运算符显示一条记录。

如果第一个条件和第二个条件中只要有一个成立,则 OR 运算符显示一条记录。




where

如需有条件地从表中选取数据,可将 WHERE 子句添加到 SELECT 语句。

SELECT 列名称 FROM 表名称 WHERE 列 运算符 值


操作符描述
=
等于
<>不等于
>大于
<小于
>=
大于等于
<=小于等于
between

在某个范围内

like
搜索某种范围




alter table ---  修改数据库表结构 

alter table database.owner.table_name add column_name char(2) null ..... 

sp_help table_name ----  显示表已有特征 

create table table_name (name char(20), age smallint, lname varchar(30)) 

insert into table_name select ......... -----  实现删除列的方法(创建新表) 

alter table table_name drop constraint Stockname_default ----  删除 Stockname  

default 约束 






CREATE TABLE 

CREATE TABLE 表名称
(
列名称1 数据类型,
列名称2 数据类型,
列名称3 数据类型,
....)



function(/*常用函数*/) 

----统计函数---- 

AVG(字段名)  得出一个表格栏平均值 

COUNT(*|字段名)  对数据行数的统计或对某一栏有值的数据行数统计 

MAX(字段名)  取得一个表格栏最大的值 

MIN(字段名)  取得一个表格栏最小的值 

SUM(字段名)  把数据栏的值相加 

 

引用以上函数的方法: 

 

sql="select sum(字段名) as  别名  from  数据表  where  条件表达式

set rs=conn.excute(sql) 

 

  rs("别名")  获取统的计值,其它函数运用同上。 

 

--AVG 

use pangu 

select avg(e_wage) as dept_avgWage 

 

from employee 

group by dept_id 

--MAX 

--求工资最高的员工姓名 

use pangu 

select e_name 

from employee 

where e_wage = 

(select max(e_wage) 

from employee) 

--STDEV() 

--STDEV()函数返回表达式中所有数据的标准差 

--STDEVP() 

--STDEVP()函数返回总体标准差 

--VAR() 

--VAR()函数返回表达式中所有值的统计变异数 

--VARP() 

--VARP()函数返回总体变异数 

----算术函数---- 





向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI