1. 程式人生 > >mysql運算符操作

mysql運算符操作

上海 col 比較運算 values ase and data -- val

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運算符操作