首页 >> 编程狮 >> 数据库

mysql入门(二)

1、空值检查

在创建表时,表设计人员可以指定其中的列是否可以不包含值。在一个列

不包含值时,称其为包含空值null。

select cust_id from customers where cust_email is null;查找用户邮箱

为空的用户编号

2、and操作符

select prod_id,prod_price,prod_name from products where 

vend_id=1003  and prod_price <=10;

3、or操作符

select prod_name,prod_price from products where vend_id = 1002

or vend_id = 1003;

4、and操作符优先级比or高

select prod_name,prod_price from products where (vend_id=1002 or 

vend_id=1003) and prod_price>=10;

检索供应商是1002或者1003,并且产品价格超过10的产品名称、产品价格

5、in操作符

select prod_name,prod_price from products where vend_id in (1002,1003)

order by prod_name;

6、not操作符(否定后跟条件)

select prod_name,prod_price from products where vend_id not in (1002,

1003) order by prod_name;

7、%通配符(%表示任何字符出现任意次数)

在搜索子句中使用通配符,必须使用like操作符

select prod_id,prod_name from products where prod_name like 'jet%';

检索以jet开头的产品

8、下划线(_)通配符 下划线只匹配单子字符

select prod_id,prod_name from products where prod_name like '_ton anvil';

9、concat()函数

select concat(vend_name,'(',vend_country,')') from vendors order by vend_name;

使用别名

select concat(vend_name,'(',vend_country,')') as vend_title from vendors 

order by vend_name;

10、执行数学计算

select prod_id,quantity,item_price, quantity*item_price as totle_price 

from orderitems where order_num = 20005;

11、日期和时间处理函数

select cust_id,order_num from orders where Date(order_date) = '2005-09-01';

12、检索出2005年9月下的所有订单

方法一:select cust_id,order_num from orders where Date(order_date) 

between '2005-09-01' and '2005-09-30';

方法二:select cust_id,order_num from orders where Year(order_date)=2005

 and  Month(order_date)=9;






标签:

上一篇
没有了

下一篇
mysql入门(一)