mysql運算子操作

來源:互聯網
上載者:User

標籤:style   ddr   ble   mys   app   北京   base   字元   範圍   

1. 增刪改查操作

查詢:

    select * from 表名 where 欄位名 運算子 數字/字元;


修改:

   update 表名 set 欄位名=值,... where 欄位名 運算子 數字/字元


刪除:

   delete from 表名 where 欄位名 運算子 數字/字元


2. 運算子

數值比較子:

=

!=

>

>=

<

<=


字元比較子:

=

!=


邏輯運算子:

and 

or


範圍內比較:

between and

in 

not in


樣板:


create database sky;

use sky;

create table m1(

id int(11),

name char(20),

age tinyint(10),

sex enum('男','女'),

score tinyint(10),

address char(20)

) default charset=utf8;

insert into m1 values

(1,'L1',21,'男',90,'北京'),

(2,'L2',19,'男',91,'上海'),

(3,'L3',24,'女',95,'廣州'),

(4,'L4',22,'男',89,'廣州'),

(5,'L5',20,'女',86,'上海'),

(6,'L6',19,'女',99,'廣州');



尋找 表裡 id在1到小於5之間的人

mysql> select * from m1 where id >=1 and id <5;

+------+------+------+------+-------+---------+

| id   | name | age  | sex  | score | address |

+------+------+------+------+-------+---------+

|    2 | L2   |   19 | 男   |    91 | 上海    |

|    3 | L3   |   24 | 女   |    95 | 廣州    |

|    4 | L4   |   22 | 男   |    89 | 廣州    |

|    1 | L1   |   21 | 男   |    90 | 北京    |

+------+------+------+------+-------+---------+

4 rows in set (0.00 sec)



欄位名 between 值1 and 值2

欄位名 in(值1,值2,...)

欄位名 not in(值1,值2,...)

尋找id在2到5之間的人並且地址在廣州的人

mysql> select * from m1 where id between 2 and 5 and address="廣州";

+------+------+------+------+-------+---------+

| id   | name | age  | sex  | score | address |

+------+------+------+------+-------+---------+

|    3 | L3   |   24 | 女   |    95 | 廣州    |

|    4 | L4   |   22 | 男   |    89 | 廣州    |

+------+------+------+------+-------+---------+

2 rows in set (0.00 sec)

或者

mysql> select * from m1 where (id between 2 and 5) and (address="廣州");

+------+------+------+------+-------+---------+

| id   | name | age  | sex  | score | address |

+------+------+------+------+-------+---------+

|    3 | L3   |   24 | 女   |    95 | 廣州    |

|    4 | L4   |   22 | 男   |    89 | 廣州    |

+------+------+------+------+-------+---------+

2 rows in set (0.00 sec)



顯示id為1,2,5的人,用  in

mysql> select * from m1 where id in(1,2,5);

+------+------+------+------+-------+---------+

| id   | name | age  | sex  | score | address |

+------+------+------+------+-------+---------+

|    2 | L2   |   19 | 男   |    91 | 上海    |

|    5 | L5   |   20 | 女   |    86 | 上海    |

|    1 | L1   |   21 | 男   |    90 | 北京    |

+------+------+------+------+-------+---------+

3 rows in set (0.00 sec



顯示地址不在北京的人

mysql> select * from m1 where address not in ("北京");

+------+------+------+------+-------+---------+

| id   | name | age  | sex  | score | address |

+------+------+------+------+-------+---------+

|    2 | L2   |   19 | 男   |    91 | 上海    |

|    3 | L3   |   24 | 女   |    95 | 廣州    |

|    4 | L4   |   22 | 男   |    89 | 廣州    |

|    5 | L5   |   20 | 女   |    86 | 上海    |

+------+------+------+------+-------+---------+

4 rows in set (0.00 sec)



顯示id為1或2或5地址在上海的和名字為L2的使用者

mysql> select * from m1 where id in(1,2,5) and address="上海" or name="L2";

+------+------+------+------+-------+---------+

| id   | name | age  | sex  | score | address |

+------+------+------+------+-------+---------+

|    2 | L2   |   19 | 男   |    91 | 上海    |

|    5 | L5   |   20 | 女   |    86 | 上海    |

+------+------+------+------+-------+---------+

2 rows in set (0.00 sec)


mysql運算子操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.