Mysql数据库练习-2 模糊搜索,排序,子查询,条件筛选,筛选为空字段

select * from products ;

Mysql数据库练习-2 模糊搜索,排序,子查询,条件筛选,筛选为空字段_第1张图片

select * from customers;

Mysql数据库练习-2 模糊搜索,排序,子查询,条件筛选,筛选为空字段_第2张图片

#1.选择出customers表中cust_email字段用户名是3个字符的客户id
# select id列名 from 数据库名 . 表名 where cust_email(列名) like '___@%'(一个_代表1个字符) 
#模糊查询(like)
#like 使用%或_来进行模糊查询
#%s 表示任意多个字符
#_表示一个任意字符
	select * from crashcourse.customers where cust_email like '___@%';
	
#2.在products表找出1003供应商的最小产品价格 
# order by 排序查询、asc升序、desc降序 asc,desc 放在语句最后面
# select * from  表名 where id列名 order by 价格列名 limit 2; 在已知情况下查询
# LIMIT是MySQL内置函数,其作用是用于限制查询结果的条数。
	select * from crashcourse.products;
    select * from crashcourse.products where vend_id='1003' order by prod_price limit 4;
 #  子查询 先执行()里面的再执行()外的 select * from 表名 where  列名 in ();
 #  MAX 函数返回一列中的最大值。MIN 函数返回一列中的最小值。
 #	IN 操作符允许我们在 WHERE 子句中规定多个值。
	select * from products where   prod_price in (
 	select min(gh.prod_price) from products gh where gh.vend_id ='1003' );
 
	select min(gh.prod_price), gh.* from products gh where gh.vend_id ='1003';
#3.在products表中找出备注字段包含‘red’的产品价格
	select * from products;
    select * from products where prod_desc like  '%red%';
    select * from crashcourse.products where prod_desc like '% red%' ;

#4.查找出products表中供应商id、产品名称、产品价格,当供应商id相同时价格由高到低排序
	# order by 排序查询、asc升序、desc降序 asc,desc 放在语句最后面
	select vend_id,prod_name,prod_price from products order by vend_id desc ,prod_name desc,prod_price desc;
	
#5.在客户表中选出cust_state是‘OHHOAZ,AC,LI,ZB,TC,WA’的客户id
#select 列名 from 表名 where 要求的列名 in ('OH','HO','AZ','AC','LI','ZB','TC','WA');  使用in
	 show tables;
    show databases;
    select * from customers;
    select cust_id from crashcourse.customers where cust_state in ('OH','HO','AZ','AC','LI','ZB','TC','WA');

#6.在客户表里选出cust_id是10005,或者cust_city是‘Detroit’的客户地址
	# and必须满足所有条件;or满足一个条件即可
	select * from customers;
    select * from customers where cust_id='10005' or cust_city='Detroit';
    
#7.在客户表中找出客户名字是'E Fudd'或者国家是'USA',并且email地址不为空的客户id.
# is 一般情况下和 null 连用,比较该字段的值是否为空
	select * from crashcourse.customers 
	where (cust_name='E Fudd' or cust_country='USA') and cust_email is NOT NULL;

#8.在产品表中找出供应商id是1003,且产品价格在2(含)和10(含)之间的供应商id,产品名称、产品价格;
	select * from products where vend_id='1003';
    select products.vend_id,products.prod_name,products.prod_price
	from crashcourse.products 
	where vend_id='1003' AND prod_price between 2 and 10;

你可能感兴趣的:(数据库学习)