mysql格式化世家_mysql语句汇总

数据库与数据库软件:

数据库是数据容器,可以理解为文件,数据库软件是数据库管理系统,

提供用户对数据库文件的操作

schama(模式):

关于数据库和表的布局及特性的信息

列(column):

表由列组成,列中存储着表中某部分的信息。

行(row):

表中的数据是按行存储的

主键:

表中每一行都应该有可以唯一标识自己的一列,称为主键

主键可以由单个列构成,也可以由多个列构成

SQL:

结构化查询语言,用于和数据库通信

查询数据库和表命令:

SHOW DATABASES;

SHOW TABLES;

SHOW COLUMNS FROM COLLATIONS; //显示COLLATIONS中的所有列

auto_increment //自动增量,每添加一个行,该值自动加一

SELECT prod_name FROM products; //从prducts中检索出prod_name

SELECT prod_id,prod_name FROM products; //检索多个列

SELECT DISTINCT vend_id from products; //返回一列中不同的值(过滤掉相同的)

SELECT prod_name FROM products LIMIT 5; //限制最多输出5行

SELECT prod_name FROM products LIMIT 4,5; //限制最多从行4输出5行

SELECT products.prod_name FROM products; //完全限定

SELECT products.prod_name FROM crashcourse.products; //完全限定(列和表)

SELECT prod_name FROM products ORDER BY prod_name; //排序

SELECT prod_id,prod_price,prod_name FROM products ORDER BY prod_price,prod_name; //对多个列排序(如果prod_price存在重复,就对prod_name重新排序)

SELECT prod_name FROM products ORDER BY prod_name DESC/ESC; //排序(升序或降序)

SELECT prod_id,prod_price,prod_name FROM products WHERE prod_price = 2.5; //使用where过滤(= 《 <> betown等操作)

SELECT prod_id,prod_price,prod_name FROM products WHERE prod_price BETWEEN 5 AND 10;//在中间

SELECT prod_id,prod_price,prod_name FROM products WHERE prod_price vend_id = 1003 AND prod_price<=10 ;//and 多个条件过滤(or任意一个条件匹配)

AND的优先级比or更高,因此,如果存在先匹配OR,再匹配AND的情况,需要用()来约束

SELECT prod_price,prod_name FROM products WHERE vend_id IN(1002,1003);//在中间

SELECT prod_id,prod_price,prod_name FROM products WHERE prod_price NOT IN(1002,1003); //起否定作用

通配符,一般用于对数据进行复杂过滤

SELECT prod_id,prod_name FROM products WHERE prod_name LIKE 'jet%'; //搜索所有以jet开头的值(也可以用在两端,比如 %jet%)

SELECT prod_id,prod_name FROM products WHERE prod_name LIKE '_jet'; //搜索所有以jet开头的值(与%的区别是只匹配一个字符)

字段拼接 Concat()函数

SELECT Concat(vend_name,'(',vend_country,')') FROM vendors ORDER BY vend_name; //生成一个由 name和country组成的报表,如果字段内部有空格,可以调用RTrim(vend_name)或LTrim(vend_name)等移除空格

别名:

上述拼接的字段没有名字,不用用于客户机应用中,可以给他们一个别名

SELECT Concat(vend_name,'(',vend_country,')') AS vend_title FROM vendors ORDER BY vend_name;

计算字段:

将检索出的数据相乘成为一个新的列

SELECT prod_id,quantity,item_price quantity*item_price AS expanded_price FROM orderitems WHERE order_num = 20005;

函数:

Upper()文本转换为大写

SELECT vend_name,Upper(vend_name) AS vend_name_upcase FROM vendors ORDER BY vend_name;

其他的文本处理函数总结

名称 调用示例 示例结果 描述

LEFT LEFT('abc123', 3) abc 返回从左边取指定长度的子串

RIGHT RIGHT('abc123', 3) 123 返回从右边取指定长度的子串

LENGTH LENGTH('abc') 3 返回字符串的长度

LOWER LOWER('ABC') abc 返回小写格式字符串

UPPER UPPER('abc') ABC 返回大写格式字符串

LTRIM LTRIM(' abc') abc 将字符串左边空格去除后返回

RTRIM RTRIM('abc ') abc 将字符串右边空格去除后返回

SUBSTRING SUBSTRING('abc123', 2, 3) bc1 从字符串第2位开始截取3位字符

CONCAT CONCAT('abc', '123', 'xyz')abc123xyz 将各个字符串参数拼接成一个新的字符串

日期和世家处理函数:

now()函数 获取当前时间

select now()

year() month() dayofmonth() 从日期中提取年月日

select * from t_student where month(birthday) = 2; //获取生日为2月份的所有学生

timestampdiff() 函数

比较两个日期之间的差值

select timestampdiff(year,birthday,now()) as age from t_student;

通过计算差值来计算出学生的年龄

to_days() 将时间转换成天数

select * from t_student where(to_days(now())-to_days(birthday))<60;

date_add 和 date_sub 根据一个日期,计算出另一个日期

select date_add('1970-1-1', interval 10 year); # 1970 年 加上10年

日期的检索:

SELECT cust_id,order_num FROM orders WHERE Date(order_date) = '2005-09-01';

SELECT cust_id,order_num FROM orders WHERE Date(order_date) BETWEEN '2005-09-01'AND '2005-09-30'

//检索某年某月的数据

SELECT cust_id,order_num FROM orders WHERE Year(order_date)=2005 AND Month(order_date) = 9;

数值处理函数:

实现数据的汇总

AVG()返回products表中所有产品的平均价格

SELECT AVG(prod_price) AS avg_price FROM products;

返回确定行或列的平均值

SELECT AVG(prod_price) AS avg_price FROM products WHERE vend_id =1003;

COUNT()函数:

用于确定表中行的数目或符合特定条件的行的数目

SELECT COUNT(*) AS num_cust FROM customers;

SELECT COUNT(cust_email) AS num_cust FROM customers; //对cust_email非空的行计数

MAX()函数

SELECT MAX(prod_price) AS max_price FROM products; //返回指定列中的最大值

MIN()函数

与MAX正好相反

SUM()用来返回指定列值的和

SELECT SUM(quantity) AS items_ordered FROM orderitems WHERE order_num = 20005;

SELECT SUM(item_price*quantity) AS total_price FROM orderitems WHERE order_num = 20005;

聚集不同值:

SELECT AVG(DISTINCT prod_price) AS avg_price FROM products WHERE vend_id = 1003;

去除掉相同的值

SELECT COUNT(*) AS num_items,MIN(prod_price) AS price_min,

MAX(prod_price) AS price_max, AVG(prod_price) AS price_avg FROM products;

分组数据:

分组数据视为了能够汇总表内容的子集

SELECT vend_id,COUNT(*) AS num_prods FROM products GROUP BY vend_id;

使用HAVING 实现GROUP BY的过滤

SELECT cust_id,COUNT(*) AS orders FROM orders GROUP BY cust_id HAVING COUNT(*)>=2;

使用ORDER BY 实现分组的排序

SELECT order_num,SUM(quantity*item_price) AS ordertotal FROM orderitems GROUP BY order_num HAVING SUM(quantity*item_price)>=50 ORDER BY ordertotal;

子查询可以用来减少查询的语句条数

例如:

SELECT order_num FROM orderitems WHERE prod_id = 'TNT2'; //返回结果20005,20007

SELECT cust_id FROM orders WHERE order_num IN(20005,20007)

SELECT cust_id FROM orders WHERE order_num IN(SELECT order_num FROM orderitems WHERE prod_id = 'TNT2');

计算字段使用子查询:

SELECT cust_name,cust_state,(SELECT COUNT(*) FROM orders WHERE orders.cust_id = customers.cust_id) AS orders FROM customers ORDER BY cust_name;

联结:

(主键与外键)

分表是为了更好的存储与提升可伸缩性,但在使用select查询的时候就不得不采用联结的方法

SELECT vend_name,prod_name,prod_price FROM vendors INNER JOIN products ON vendors.vend_id = products.vend_id'

表别名能够在单挑SELECT语句中不止一次的引用相同的表

SELECT prod_id,prod_name FROM products WHERE vend_id = (SELECT vend_id FROM products WHERE prod_id = 'DTNTR');

自联结方式:

SELECT p1.prod_id,p1.prod_name FROM products AS p1,products AS p2 WHERE p1.vend_id = p2.vend_id AND p2.prod_id = 'DTNTR';

外部联结

联结包含在相关表中没有关联行的行,这种类型的联结成为外部联结

SELECT customers.cust_id,orders.order_num FROM customers LEFT OUTER JOIN orders ON customers.cust_id = orders.cust_id;

左向外联结和右向外联结

左向外联结的结果集包括 LEFT OUTER子句中指定的左表的所有行,而不仅仅是联结所匹配的行

RIGHT JOIN 和RIGHT OUTER JOIN

右向外联结是左向联结的反向联结,将返回右表的所有行

FULL JOIN 或FULL OUTER JOIN

完整外部联结返回左表和右表的所有行,当某行在另一个表中没有匹配行时,则另一个表的

选择表列包含空值,如果表之间有匹配行,则整个结果集包含基表的数据值。

带聚集函数的联结

SELECT customers.cust_name,customers.cust_id,COUNT(orders.order_num) AS num_ord FROM customers INNER JOIN orders ON customers.cust_id = orders.cust_id GROUP BY customers.cust_id;

此SELECT语句使用INNER JOIN将customers和orders表相互关联。GROUP BY子句按客户分组数据,因此函数调用COUNT(order.order_num)对每个客户的订单技术,将它作为num_ord返回。

聚集函数也可以方便的与其他联结一起使用

SELECT customer.cust_name,customer.cust_id,COUNT(order.oder_num) AS num_ord FROM customers LEFT OUTER JOIN orders ON customers.cust_id = orders.cust_id GROUP BY customers.cust_id;

组合查询:

多个查询(多条SELECT语句),并将结果作为单个查询结果集返回

组合查询的应用场景:

在单个查询中从不同的表返回类似结构的数据

对单个表执行多个查询,按单个查询返回数据

如果使用WHERE:

SELECT vend_id,prod_id,prod_price FROM products WHERE prod_price<=5 OR vend_id IN(1001,1002);

使用UNION:

SELECT vend_id,prod_id,prod_price FROM products WHERE prod_price <=5 UNION SELECT vend_id,prod_id,prod_price FROM products WHERE vend_id IN(1001,1002);

UNION会自动去重,如果想要匹配所有行,可以使用UNION ALL

SELECT语句的输出用ORDER BY子句排序,在用UNION组合查询时,只能用一条ORDER BY子句。必须出现在最后一条SELECT语句之后。

SELECT vend_id,prod_id,prod_price FROM products WHERE prod_price <=5 UNION SELECT vend_id,prod_id,prod_price FROM products WHERE vend_id IN(1001,1002) ORDER BY vend_id,prod_price;

INSERT INFO customers(cust_name,cust_address,cust_city,cust_state,cust_zip,cust_country,cust_contact,cust_email) VALUES('Pep E.LaPew','100 Main Street','Los Angeles','CA','90046','USA',NULL,NULL);

如果想插入多行

INSERT INFO customers(cust_name,cust_address,cust_city,cust_state,cust_zip,cust_country,cust_contact,cust_email) VALUES(('Pep E.LaPew','100 Main Street','Los Angeles','CA','90046','USA',NULL,NULL)(...));

INSERT SELECT:

//将SELECT语句返回的结果插入到另一张表中,通常用于将一张表导入到另外一张表中

INSERT INFO customers(cust_name,cust_address,cust_city,cust_state,cust_zip,cust_country,cust_contact,cust_email) SELECT cust_name,cust_address,cust_city,cust_state,cust_zip,cust_country,cust_contact,cust_email FROM custnew;

更新和删除数据:

更新某一行的数据:

UPDATE customers SET cust_mail = '[email protected]' WHERE cust_id = 10005;

更新多行:

UPDATE customers SET cust_mail = '[email protected]' ,cust_email = '[email protected]' WHERE cust_id = 10005;

IGNORE关键字可以忽略更新过程中的错误

删除数据:

DELETE FROM customers WHERE cust_id = 10006;

创建表:

CREATE TABLE customers

(

cust_id int NOT NULL AUTO_INCREMENT,

cust_name char(50) NOT NULL,

cust_address char(50) NULL,

cust_city char(50) NULL,

cust_state char(5) NULL,

cust_zip char(10) NULL,

cust_country char(50) NULL,

cust_contact char(50) NULL,

cust_email char(255) NULL,

PRIMARY KEY(cust_id)

)ENGINE=InnoDB;

NULL值就是没有值或缺值。

更新表ALTER

ALTER TABLE vendors ADD vend_phone CHAR(20);

删除刚刚添加的列:

ALTER TABLE Vendors DROP COLUMN vend_phone;

删除表:

DROP TABLE customers;

重命名表:

RENAME TABLE customers2 TO customers;

视图:

视图是虚拟的表,只包含使用时动态检索数据的查询

使用视图:

使用视图的原因:

重用SQL语句;

简化复杂的SQL语句,在编写查询后,可以方便的重用它而不必知道它的基本查询细节

使用表的组成部分而不是整个表

保护数据,可以给用户授予表的特定部分的访问权限而不是整个表的访问权限

更改数据格式和表示。视图可返回与底层表的表示和格式不同的数据。

触发器:

触发器的主要作用是事件发生时某些语句自动执行

CREATE TRIGGER newproduct AFTER INSERT ON products FOR EACH ROW SELECT 'Product added';

你可能感兴趣的:(mysql格式化世家)