mysql -u root -p -h localhost -P 3306
select prod_name from products limit 3;
select prod_name from products limit 3, 2;
当一个参数时,是从0行开始的
当两个参数时,也是从0行开始的
select prod_name from products order by prod_name;
多列排序规则,先按第一列排序,然后在按第二列排序
例如先按照姓排序,然后在按照名排序
select prod_id, prod_price, prod_name from products order by prod_price, prod_name;
默认是按照升序进行排序
降序排序需要使用DESC
DESC
只运用在写在它前面的字段,如果想作用在多列,则每一列都需要加上DESC
select prod_id, prod_price, prod_name from products order by prod_price DESC;
升序排序的关键字是ASC
order by应该在from后面
limit 应该在order by后面
ORDER BY应该位于WHERE后面
操作符 | 说明 |
---|---|
= | 等于 |
<> | 不等于 |
!= | 不等于 |
< | 小于 |
> | 大于 |
<= | 小于等于 |
>= | 大于等于 |
BETWEEN | 在指定的两个值之间 |
is null | 值是否为null |
select cust_id from customers where cust_email is null;
mysql顺序是先解决and 再 解决or
可以使用()
select prod_id, vend_id, prod_price from products where (vend_id = 1001 or vend_id = 1003) and prod_price > 10;
指定条件范围
select prod_name, prod_price from products where vend_id in (1001, 1003) order by prod_name limit 1;
not可以与in、between、exists配合使用
like ‘x%h’
like ‘%h’
like ‘x%’
可以匹配0、1、多个字符
xixixi空格
用 %xi
会影响匹配结果
% 不能匹配 null
只能匹配一个字符,不多不少
select concat(vend_name, '(', vend_address, ')') from vendors;
该函数将接受的字段通过'字段一','字段二'
连接
ltrim
rtrim
trim
函数 | 说明 |
---|---|
Left | 返回串左边的字符 |
Length | 返回字符串的长度 |
Locate | 找出串的一个子串 |
Lower | 将串转换为小写 |
Right | 返回串右边的字符 |
SubString | 返回字串的 字符 |
Upper | 将串转换为大写 |
select cust_id, order_num from orders where date(order_date) > '2005-09-30';
select p.prod_name, p.prod_price, pr.note_text from products AS p, productnotes AS pr where p.prod_id = pr.prod_id and p.prod_id = 'TNT2';
操作符 | 说明 |
---|---|
+ | 加 |
- | 减 |
* | 乘 |
/ | 除 |
函数 | 说明 | 备注 |
---|---|---|
avg | 平均值 | 忽略值为null的行 |
count | 行数 | count(*) 统计包含null的值。count(column) 统计不包含null的值 |
max | 最大值 | |
min | 最小值 | |
sum | 和 |
select vend_id, count(*) from products group by vend_id with rollup;
group by 必须出现在where子句之后。order by 子句之前
select order_num ,count(*) AS count from orderitems group by order_num ORDER BY count;
select vend_id, count(*) from products where prod_price >= 10 group by vend_id having count(*) >= 2;
where在数据分组前过滤
having在数据分组后过滤
顺序 |
---|
select |
from |
where |
group by |
having |
order by |
limit |
查找订购物品为TNT2的所有客户的名字。
select cust_name. cust_contact from customers where cust_id in (select cust_id from orders where order_num in (select order_num from orderitems where prod_id ='TNT2' )); ERROR 1054 (42S22): Unknown column 'cust_name.cust_contact' in 'field list' mysql> select cust_name, cust_contact from customers where cust_id in (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;
外键为某个表中的一列,它包含另一个表中的主键值
又称等值联结
select vendors.vend_id, vend_name, prod_name , prod_price from vendors inner join products on vendors.vend_id = products.vend_id;
显示编号为20005的订单中的物品
select products.prod_name, products.prod_price, orderitems.quantity from products, orders, orderitems where orderitems.prod_id = products.prod_id and orders.order_num = orderitems.order_num and orders.order_num = '20005';
查询ID为DTNTR供应商的其它产品
select p2.prod_id, p2.prod_name from products AS p1, products AS p2 where p1.vend_id = p2.vend_id and p1.prod_id = 'DTNTR';
select p2.prod_id, p2.prod_name from products AS p1, products AS p2 where p1.vend_id = p2.vend_id and p2.prod_id = 'DTNTR';
只是查一个表,所以from products AS p1, products AS p2 要定义成两个别名,要不然mysql也不知道prod_id和prod_name是哪个表
and结尾 p2.prod_id = ‘DTNTR’、p1.prod_id = 'DTNTR’显示的结果不同
select customers.cust_id, orders.order_num from customers LEFT OUTER join orders on customers.cust_id = orders.cust_id ;
RIGHT OUTER JOIN
select customers.cust_name, customers.cust_id, count(*) AS order_count from customers inner join orders on customers.cust_id = orders.cust_id group by customers.cust_id ;
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);
将两个select语句进行查询
可以理解成where 条件1 or 条件2
自动去掉重复行
即使有重复行也会显示出来
与select区别,select也会自动去除重复行
所以如果需求是保留重复行,那么只能用UNION ALL,这是他俩却别
要作用在最后一个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;
两个select查询,每个select查询的列应该相同,顺序可以不一致
ENGINE=MyISAM
InnoDB不支持全文搜索
CREATE TABLE productnotes ( note_id int NOT NULL AUTO_INCREMENT, prod_id char(10) NOT NULL, note_date datetime NOT NULL, note_text text NULL , PRIMARY KEY(note_id), FULLTEXT(note_text) ) ENGINE=MyISAM;
使用FULLTEXT关键字
可以指定多个列
match(列名)
against(搜索表达式)
select note_text from productnotes where match(note_text) against('rabbit');
在全文本搜索中,会根据查找的关键字,维护了一个等级,最先出现的,频率高的,等级也会高。
略
INSERT INTO customers VALUES(null, 'Coyote Inc.', '200 Maple Lane', 'Detroit', 'MI', '44444', 'USA', 'Y Lee', '[email protected]');
INSERT INTO customers (cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email) VALUES('Coyote Inc.', '200 Maple Lane', 'Detroit', 'MI', '44444', 'USA', 'Y Lee', '[email protected]');
update customers set cust_contact = 'yaoyan', cust_email = '[email protected]' where cust_id = 10001;
delete from customers where cust_id = 10005;
truncate table
Cannot delete or update a parent row: a foreign key constraint fails
SET foreign_key_checks = 0; // 先设置外键约束检查关闭
drop table mytable; // 删除数据,表或者视图
SET foreign_key_checks = 1; // 开启外键约束检查,以保持表结构完整性
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;
‘’ 中间无值,在not null是允许的
quantity int NOT NULL default 1
用于事物
用在内存
用于文本搜索
主键必须唯一
这些列的组合值必须唯一
PRIMARY KEY (列名1, 列名2)
select last_insert_id();
alert table vendors add vend_phone char(20);
alert table vendors drop column vend_phone;
ALTER TABLE orders ADD CONSTRAINT fk_orders_customers FOREIGN KEY (cust_id) REFERENCES customers (cust_id);
drop table tablename;
rename table 久的表名 to 新的表名
create view productcustomers as select products.prod_name, products.prod_price, orderitems.quantity from products, orders, orderitems where orderitems.prod_id = products.prod_id and orders.order_num = orderitems.order_num;
show create view productcustomers;
drop view productcustomers;
视图用于检索,不是用于更新
rollback
回退start transaction
之后的所有语句
start transaction;
delete from orderitems where order_num = '20006';
commit;
savepoint delete1;
rollback to delete1
create user ben identified by 'yaoyan';
drop user ben;
show grants for ben;
ON . 代表没有任何权限
grant select ON study_mysql.* to ben;
授权ben用户可以在study_mysql数据库里面的所有表使用select命令
revoke select on study_mysql.* from ben;
alter user ben identified by '1234';
参考文章