2. MySQL语句

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 2. MySQL语句

选择语句

sql

复制代码

use sql_store;
select *
from customers;
-- where customer_id = 2
-- order by points
select 
  last_name,
    first_name,
    points,
    points + 10,    -- 各种运算都可以
    (points + 10) * 100 as 'discount factor'    -- 起别名,有空格加单引号
from customers;
select distinct state   -- distinct 不重复字段 
from customers;
-- return all the products
-- name
-- unit price
-- new price (unit price * 1.0)
select name,unit_price,unit_price * 1.1 as new_price
from products;

where语句及相关

sql

复制代码

-- 比较符号
select *
from customers
where points > 3000 and state <> 'TX' or points = 3000 and state = 'TX';    
select * 
from ordes
where order_date >= '2019-01-01';
-- not
select * 
from ordes
where not state = 'IL';
select *
from order_items
where order_id = 6 and unit_price * quantity > 30;
-- in 范围
select *
from customers
where state not in ('VA','TX');     
-- between
select * 
from customers
where birth_date between '1990-01-02' and '2000-01-03';
-- like
-- %是*
-- _是?
select *
from customers
where last_name like '%b%' and 
    address not like '%trail%';
      
-- regexp 正则表达式
-- ^ 表示开始
-- $ 表示结束
-- | 逻辑或
-- [abcd]
-- [a-f]
select * 
from customers
where last_name like '%field%' and
    last_name regexp 'field' or
      last_name regexp '^field|mac|rose' or   -- 以field开头或者包含mac或rose
      last_name regexp '[a-h]e';
-- is null
select *
from customers
where phone is null;
-- order by
select *
from customers
order by state desc,first_name;
select birth_date, first_name, last_name, 10 as points
from customers
order by 1,2;   -- 相对顺序,从1开始
SELECT *,quantity * unit_price AS total_price
FROM order_items
WHERE order_id = 2
ORDER BY total_price DESC;
-- limit 限制数量
-- offset 偏移量
-- 可以结合使用page
select *
from customers
limit 3
offset 3

创建数据库

ini

复制代码

CREATE DATABASE 数据库名;

DELETE 语句

sql

复制代码

DELETE FROM table_name [WHERE Clause]
  • 如果没有指定 WHERE 子句,MySQL 表中的所有记录将被删除。
  • 你可以在 WHERE 子句中指定任何条件
  • 您可以在单个表中一次性删除记录。


相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
9月前
|
SQL 关系型数据库 MySQL
MySQL常用语句
MySQL常用语句
205 0
|
5天前
|
SQL 存储 关系型数据库
MySQL特点和基本语句
MySQL特点和基本语句
34 0
|
6月前
|
关系型数据库 MySQL
MySQL8 with语句
MySQL8 with语句
|
7月前
|
关系型数据库 MySQL
最常用MySQL语句
最常用MySQL语句
58 1
|
7月前
|
关系型数据库 MySQL 数据库
MySQL—常用语句汇总
MySQL—常用语句汇总
|
7月前
|
SQL 关系型数据库 MySQL
|
8月前
|
SQL 关系型数据库 MySQL
mysql的语句详解
mysql的语句详解
60 1
|
关系型数据库 MySQL
MYSQL语句。
MYSQL语句。
79 0
MYSQL语句。
|
SQL 存储 关系型数据库
MySQL实用语句及小技巧
前言 我们在之前已经过了一遍sql的基本操作, 那么那些基本操作如何使用、如何排列才能最大程度发挥服务器的性能呢?如何才能把他们连接起来成为能为我们所服务的强大数据库系统呢? 本期我们就将引入一些配合sql核心语句的使用技巧和一些灵活的理解
|
关系型数据库 MySQL 数据库
常用的MySQL语句
常用的MySQL语句
70 0
常用的MySQL语句