基础SQL语句

store_information表格:                                                                   Geography表格:

store_name sales date
Los Angeles 1500 1999-01-05
San Diego 250 1999-01-07
Los Angeles 300 1999-01-08
Boston 700 1999-01-08
region_name store_name
East Boston
East New York
West Los Angeles
West San Diego

 

 

 

 

1. 查询:

# 基本查询
SELECT store_name FROM store_information
#DISTINCT去除重复行
SELECT DISTINCT store_name FROM store_information
#WHERE条件查询
SELECT store_name FROM store_information WHERE sales > 300
#AND/OR增加条件
SELECT store_name FROM store_information WHERE sales > 300 and sales < 1500
#IN指定查询值
SELECT store_name FROM store_information WHERE store_name IN ('Los Angeles', 'Boston')
#BETWEEN指定查询范围
SELECT store_name FROM store_information WHERE sales BETWEEN 300 AND 700
#LIKE匹配
SELECT store_name FROM store_information WHERE store_name LIKE '%AN%' #store_name中含有AN的记录
#ORDER BY排序
SELECT store_name, sales FROM store_information ORDER BY 2 DESC #DESC/ASC降序/升序, 2指按照第二个参数(sales)
#利用函数:AVG(平均),COUNT(计数),MAX(最大值)
SELECT SUM(sales) FROM store_information
SELECT COUNT(DISTINCT store_name) FROM store_information WHERE store_name is not NULL
#GROUP BY分组
SELECT store_name, SUM(sales) FROM store_information GROUP BY store_name
#HAVING对运算结果进行选择
SELECT store_name, SUM(sales) FROM store_information GROUP BY store_name HAVING SUM(sales) > 1500
#别名
SELECT store_name, SUM(sales) "Total Sales" FROM store_information GROUP BY store_name #结果一样,不过SUM(sales)被显示为Total Sales
SELECT store_name, SUM(sales) TotalSales FROM store_information GROUP BY store_name
#连接
SELECT A1.region_name REGION, SUM(A2.sales) SALES FROM geography A1, store_information A2
  WHERE A1.store_name = A2.store_name GROUP BY A1.region_name
#外部连接
#上面那个连接是需要两个表都有相同的内容才能显示,即内部连接(又称左连接: left join).要想列出某个表格中的所有资料,需要用外部连接:
SELECT A1.store_name, SUM(A2.sales) SALES FROM geography A1, store_information A2
WHERE A1.store_name = A2.store_name(+) GROUP BY A1.store_name #oracle用+表示外部连接,mysql还不支持
#subquery
SELECT SUM(sales) FROM store_information WHERE store_name IN (SELECT store_name
FROM geography WHERE region_name = 'West') #相当于是嵌套
#UNION将两个SQL语句的合并起来 ## UNION要求两个SQL语句的字段相同,并且UNION只会显示不同的资料值(类似SELECT DISTINCT)
SELECT store_name FROM store_information UNION SELECT store_name FROM geography
SELECT store_name FROM store_information UNION ALL SELECT store_name FROM geography #UNION ALL列出每一笔符合条件的资料,无论资料值是否重复 #CONCAT()字段拼接
SELECT CONCAT(region_name, store_name) FROM geography WHERE store_name = 'Boston' #mysql语法,结果:'EastBoston'
#SUBSTRING()字段裁剪 (mysql)
SELECT SUBSTRING(region_name, 2,4) FROM geography #(下标从1开始),查出region_name的第2到第4个字母
#TRIM
SELECT TRIM(' Sample ') #结果: 'Sample'
SELECT LTRIM(' Sample ') #去除左边空格
SELECT RTRIM(' Sample ') #去除右边空格

2. 修改:

#建表
create table geography
(
  geographyid  INT,
  region_name  varchar(20),
  store_name   varchar(20)
)
#建视图
##视图是虚拟表格,它是建立在表格之上的一个架构。而表格代表了实际存储的资料
CREATE VIEW V_REGION_SALES
AS SELECT A1.region_name REGION, SUM(A2.sales) SALES
FROM geography A1, store_information A2
WHERE A1.store_name = A2.store_name
GROUP BY A1.region_name;
SELECT * FROM V_REGION_SALES; #建索引
#建索引是为了加快查询,没有索引的表需要查询表格里所有的记录,有索引的先根据索引检索
CREATE INDEX IDX_STORE_NAME ON geography(store_name)
#改变表格
# ADD: 增加 DROP: 删除 CHANGE: 改变名称 MODIFY: 改变种类
ALTER TABLE geography ADD zipcode char(6)
ALTER TABLE geography CHANGE region_name regionname varchar(30)  #改变名称,需要给出名称类型

ALTER TABLE geography MODIFY regionname varchar(20)  #改变类型
ALTER TABLE geography DROP regionname
#设置主键
#主键可以在建表时设置,也可以用ALTER TABLE设置 #设置为主键后,该值默认也会被设置为NOT NULL
CREATE TABLE test
(
  testid int primary key,
  testdata varchar(20)
)
CREATE TABLE test2
(
  test2id int,
  test2data varchar(20),
  primary key (test2id)
)
ALTER TABLE test3 ADD primary key (test3id)
#外键 #法1
CREATE TABLE test1
(
  test1id INT(11) primary key not null,
  test1data varchar(20)
)
CREATE TABLE test2
(
  test2id INT(11) primary key not null,
  fid INT(11) not null,
  test2data varchar(20),
  foreign key (fid) references test1(test1id)
)
#法2
ALTER TABLE test2 ADD FOREIGN KEY (fid) REFERENCES test1(test1id)
#删除表格
DROP TABLE test2  #删除表格后,此表格将不存在
#清楚表格内容
TRUNCATE TABLE geography
#插值
INSERT INTO test1(test1id, test1data) VALUES(1,"testdata")  #插入一条数据
INSERT INTO store_information(store_name, sales, date) SELECT
store_name, sales, date FROM sales_information WHERE year(date) = 1998  #插入多条数据(从一个表里将数据都找出来然后插入另一个表里)
#UPDATE记录
UPDATE test1 SET test1data = "message" WHERE test1id = 1
#DELETE记录
DELETE FROM test1 WHERE test1id = 1

 

转载于:https://www.cnblogs.com/wangzhijun/archive/2013/03/19/2966471.html

你可能感兴趣的:(基础SQL语句)