mysql 必知必会的一些说明(未完~)

所有数据:

Products 所有数据

prod_id vend_id prod_name prod_price
BNBG01 DLL01 Fish bean bag toy 34900.00
BNBG02 DLL01 Bird bean bag toy 34900.00
BNBG03 DLL01 Rabbit bean bag toy 34900.00
BR01 BRS01 8 inch teddy bear 59900.00
BR02 BRS01 12 inch teddy bear 89900.00
BR03 BRS01 18 inch teddy bear 119900.00
RGAN01 DLL01 Raggedy Ann 49900.00
RYL01 FNG01 King doll 94900.00
RYL02 FNG01 Queen doll 94900.00

from

  • 1.普通使用方式:

    • SELECT  prod_name FROM Products;
       
  • 2.完全限定表名

    • SELECT  Products.prod_name FROM Products;

distinct

此关键字指示MySQL 只返回不同的值。

用法 SELECT DISTINCT vend_id FROM Products

结果:

    • vend_id
      DLL01
      BRS01
      FNG01

      说明:DISTINCT必须放在列名前面 ,且 不可以再指定其他列名;

      如:SELECT DISTINCT vend_id, prod_price from Products这个是不可以的;

limit

  • select * from Products limit 5;这里LIMIT 5指示MySQL返回 不多于5
  • 结果:
  • prod_id vend_id prod_name prod_price
    BNBG01 DLL01 Fish bean bag toy 34900.00
    BNBG02 DLL01 Bird bean bag toy 34900.00
    BNBG03 DLL01 Rabbit bean bag toy 34900.00
    BR01 BRS01 8 inch teddy bear 59900.00
    BR02 BRS01 12 inch teddy bear 89900.00
  • select * from Products limit 5,5;指示MySQL返回从行5开始的5行数据即:limit startRowNum(开始的行号),getRowsCount(获取行数);
  • 结果:
    prod_id vend_id prod_name prod_price
    BR03 BRS01 18 inch teddy bear 119900.00
    RGAN01 DLL01 Raggedy Ann 49900.00
    RYL01 FNG01 King doll 94900.00
    RYL02 FNG01 Queen doll 94900.00
  • 注意:获取的数据范围:(startRowNumgetRowsCount+startRowNum]
  • select * from Products limit 4,3指示MySQL返回从行4开始的3行,等同于 select * from Products limit 3 OFFSET 4;

  结果:

prod_id vend_id prod_name prod_price
BR02 BRS01 12 inch teddy bear 89900.00
BR03 BRS01 18 inch teddy bear 119900.00
RGAN01 DLL01 Raggedy Ann 49900.00

order by

    • 单个排序:SELECT * FROM Products ORDER BY prod_name;按照产品名称排序(正序)
    • 多个排序:SELECT * FROM Products ORDER BY prod_price,prod_id;按照产品价格排序(正序),如果产品价格相同按照 产品ID排序(正序)
      指定方向:SELECT * FROM Products ORDER BY prod_price desc,prod_id asc; 按照产品价格排序(倒叙),如果产品价格相同按照 产品ID排序(正序)

 where

空值判断(null):

select * from Products where prod_price is null;

数据过滤(and or) :

and:

select * from Products where vend_id='DLL01' and prod_price<=94900.00

用在WHERE子句中的关键字,用来指示检索满足所有给定 条件的行。

上述例子中使用了只包含一个关键字AND的语句,把两个过滤条件组 合在一起。还可以添加多个过滤条件,每添加一条就要使用一个AND

or:

select * from Products where vend_id='DLL01' or vend_id='BRS01'

OR操作符与AND操作符不同,它指示MySQL检索匹配任一条件的行,

and or 组合:

        select * from Products where vend_id='DLL01' or vend_id='BRS01' and prod_price>89900.00

结果是

prod_id vend_id prod_name prod_price
BNBG01       DLL01 
  Fish bean bag toy 
    34900.00
 BNBG02
DLL01
Bird bean bag toy     34900.00
BNBG03 DLL01 Rabbit bean bag toy   34900.00
BR03 BRS01 18 inch teddy bear
119900.00   
RGAN01   DLL01 Raggedy Ann 49900.00   

原因是 优先级 and 高于 or

mysql 默认是 :    select * from Products where vend_id='DLL01' or (vend_id='BRS01' and prod_price>89900.00)

所以需要手动加括号  select * from Products where (vend_id='DLL01' or vend_id='BRS01') and prod_price>89900.00

in:

圆括号在WHERE子句中还有另外一种用法。IN操作符用来指定条件范 围,范围中的每个条件都可以进行匹配。IN取合法值的由逗号分隔的清 单,全都括在圆括号中。

select * from Products where vend_id in('DLL01','BRS01'):表示 vend_id 是 DLL01 或者 BRS01

not:

WHERE子句中的NOT操作符有且只有一个功能,那就是否定它之后所 跟的任何条件。

select * from Products where vend_id not in('DLL01','BRS01'):表示 vend_id 不是 DLL01 和 BRS01

like:

LIKE指示MySQL后跟的搜索模式利用通配符匹配而不是直接相等匹配进行比较。

 

最常使用的通配符是百分号(%)。在搜索串中,%表示任何字符出现 任意次数。例如,为了找出所有以词King起头的产品,可使用以下SELECT 语句:

        SELECT * FROM PRODUCTS WHERE PROD_NAME LIKE 'King%'

sql SELECT 和 select 是一样的,那是 LIKE 'King%' 和 LIKE 'king%' 是不一样的;

条件操作符

操作符

说明

例子

=

等于

select * from Products where prod_name ='Fish bean bag toy'

<

小于

select * from Products where prod_price <94900.00

<>

不等于

select * from Products where prod_id<>'BR03'

!=

不等于

select * from Products where prod_id!='BR03'

<=

小于等于

select * from Products where prod_price <=94900.00

>=

大于等于

between

在两者之间(取值区间 [btn1,btn2])

所需范围的低端值和高端值

select * from Products where prod_price between 94900.00 and 34900.00

 拼接字段CONCAT

select CONCAT(vend_name,'(',vend_country,')') from Vendors order by vend_name

 别名:

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

IFNULL(expr1,expr2)

含义是:如果第一个参数不为空,则返回第一个参数,否则返回第二个参数。

 IF(expr1,expr2,expr3):

含义是:如果第一个表达式的值为TRUE(不为0或null),则返回第二个参数的值,否则返回第三个参数的值。

创建表 :

创建表名为 Products的数据表

CREATE TABLE `demo_treeinfo` (
`ID` int(11) not null AUTO_INCREMENT,-- 类型为 int 长度为11 不能为空 自增
`PID` VARCHAR(50) NOT NULL,
`NAME` VARCHAR(50) DEFAULT NULL,,-- 类型为  VARCHAR长度为50 默认为空 如果不为空 则是`NAME` VARCHAR(50) DEFAULT ‘111’
`OPEN` CHAR(2) DEFAULT NULL,
`CREATOR` VARCHAR(50) DEFAULT NULL,
`MODIFIER` VARCHAR(50) DEFAULT NULL,
`CREATE_DATE` datetime DEFAULT NULL,
`MODIFY_DATE` datetime DEFAULT NULL,
`IS_DELETED` CHAR(2) DEFAULT NULL,
PRIMARY KEY (`ID`))

修改表:

添加列

ALTER TABLE VendorsCopy
ADD vend_phone CHAR(20) DEFAULT 'a';

删除列:

ALTER TABLE Vendors DROP COLUMN vend_phone;

插入数据

INSERT INTO `Customers` VALUES ('1000000001', 'Village Toys', '200 Maple Lane', 'Detroit', 'MI', '44444', 'USA', 'John Smith', 'sales@villagetoys.com');
 
存储到表中每一列的数据在VALUES子句中给出,必须给每一列提供一个值。如果某列 没有值,如上面的cust_contactcust_email列,则应该使用NULL值(假定表允许对该列指定空值)

 如果需要特殊指定:

INSERT INTO Customers(cust_id, cust_name,
cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email)
VALUES('1000000006', 'Toy Land',
'123 Any Street', 'New York',
'NY',
'11111',
'USA', NULL, NULL);

 插入检索出的数据:

INSERT通常只插入一行。要插入多行,必须执行多个INSERT语句。INSERT SELECT是个例外,它可以用一条INSERT插入多行,不 管SELECT语句返回多少行,都将被INSERT插入。

INSERT INTO Customers(cust_id, cust_contact,
cust_email, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country)
SELECT cust_id,
cust_contact, cust_email, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country
FROM CustNew;

从一个表复制到另一个表

CREATE TABLE CustCopy AS SELECT * FROM Customers;
这条SELECT语句创建一个名为CustCopy的新表,并把Customers表的整个内容复制到新表中。因为这里使用的是SELECT *,所以将 在CustCopy表中创建(并填充)与Customers表的每一列相同的列

修改数据

   
UPDATE Customers
SET cust_email = 'kim@thetoystore.com' WHERE cust_id = '1000000005';

删除数据

 DELETE FROM CustCopy WHERE cust_id = '1000000006';

创建视图:

视图名称为ProductCustomers

CREATE VIEW ProductCustomers AS SELECT cust_name, cust_contact, prod_id FROM Customers, Orders, OrderItems WHERE Customers.cust_id = Orders.cust_id
AND OrderItems.order_num = Orders.order_num;

创建索引

CREATE INDEX indexName ON table_name (column_name)
我从来不相信什么懒洋洋的自由。我向往的自由是通过勤奋和努力实现的更广阔的人生。 我要做一个自由又自律的人,靠势必实现的决心认真地活着。
原文地址:https://www.cnblogs.com/lixiuming521125/p/14993686.html