mysql -uroot -p show databases; create database gj use gj create table location (Region char(20),Store_Name char(20)); insert into location values ('East','Boston'); insert into location values ('East','New York'); insert into location values('West','Los Angeles'); insert into location values('West','Houston'); select * from location; create table store_info(Store_Name char(20),Sales int(10),Date char(10)); mysql> insert into store_info values('Los Angeles','1500','2020-12-05'); mysql> insert into store_info values('Houston','250','2020-12-07'); mysql> insert into store_info values('Los Angeles','300','2020-12-08'); mysql> insert into store_info values('Boston','700','2020-12-08'); mysql> select * from store_info;
select Sales from store_info;
select distinct Store_Name from store_info;
select Store_Name from store_info where Sales > 1000;
SELECT Store_Name FROM store_info WHERE Sales >1000 OR (Sales < 500 AND Sales > 200);
SELECT * FROM store_info WHERE Store_Name IN ('Los Angeles','Houston');
SELECT * FROM store_info WHERE Date BETWEEN '2020-12-06' AND '2020-12-10';
'A_Z':所有以'A'起头,另一个任何值的字符,且以 'z' 为结尾的字符串。例如,'ABZ' 和 'A2Z' 都符合这个模式,而 'AKKZ'并不符合(因为在A和Z之间有两个字符,而不是一个字符)。 'ABC%'∶所有以'ABC'起头的字符串。例如 'ABCD'和 'ABCABC'都符合这个模式。'%XYZ '∶所有以 'XYZ' 结尾的字符串。例如 'WXYZ'和 'ZZXYZ' 都符合这个模式。 '%AN%'∶所有含有'AN'这个模式的字符串。例如 'LOS ANGELES' 和 'SAN ERANCISCO' 都符合这个模式。 '_AN%'∶所有第二个字母为'A'和第三个字母为 'N'的字符串。例如 'SAN FRANCISCO'符合这个模式,而 'LOS ANGELES'则不符合这个模式。
SELECT * FROM Store_info WHERE Store_Name like '%os%'; #模糊匹配,显示出Store_Name该字段符合like通配符内容的数据
SELECT Store_Name, Sales,Date FROM store_info ORDER BY Sales DESC;
select Store_Name, sum(sales) from store_info GROUP BY Store_Name ORDER BY sales s desc;
select Store_Name, SUM(Sales) from store_info GROUP BY Store_Name HAVING sum(Salesles) > 1500;
SELECT abs(-1),rand(),mod(5, 3),power(2,3),round(1.89),round (1.89); select round(1.8937,3),truncate(1.235,2),ceil(5.2),floor(2.1),least(1.89,36.1,2.1);
SELECT avg (Sales)FROM store_info; SELECT count(DISTINCT Store_Name)FROM store_info; SELECT max(Sales) FROM store_info; SELECT min(Sales) FROM store_info; SELECT sum(Sales) FROM store_info;
select concat (Region, Store_name ) from location where Store_Name = 'Boston';
SELECT Region || ' ' || Store_Name FROM location WHERE Store_Name ='Boston';
SELECT substr(Store_Name, 3) FROM location WHERE Store_Name ='Los Angeles'; SELECT substr(Store_Name, 2,4) FROM location WHERE Store_Name = 'New York';