第2章 查询基础

简介:

  1、select 语句基础

  2、算数运算符和比较运算符

  3、逻辑运算符

一、select 语句基础

-- select 列名 from 表名,也可以用select * from 表名
select product_id from product;
select * from product;

-- 使用别名显示,中文为什么这个数据库要用双引号啊,真是尴尬。。。
select product_id as id_num,product_name as "名字" from product;

-- 查询常数,这里又需要用单个引号,也是醉了
select '商品' as string,'2018-6-10' as date,product_id as id_num from product;

-- 去除重复值用distinct,如果是两列的话,要两列都一样才会去重
select distinct product_type,regist_date from product;

-- where语句来作为条件判断,判断时候先用where筛选出行,再查找select中需要的列
select * from product where product_name='打孔器';

注释的书写方式有两种,一种是-- 一种是/*  */

-- 这种注释也可以
/*
这是注释第一行
这是第二行注释
这是第三行
*/

二、算数运算符和比较运算符

算数运算符主要有+ - * /四种,其中要注意的是不管任何值与null计算都是null

比较运算符有>  <  =  >=   <=   <>  分别是大于,小于,等于,大于等于,小于等于,不等于。也不能够跟null进行比较

判断是否是null值用is null或者is not null

select * from product;
select sale_price * 2 as double_price,sale_price from product;
select * from product where sale_price = 1000;

select * from product where purchase_price is null;

三、逻辑运算符

逻辑运算符是指not,or,and运算符,and的运算优先级大于or,所以有时候条件多个时候我们要用()

总结:本章学习了基础的select知识

作业:

  1、select product_name,regist_date from product where regist_date > '2009-4-28'

  2、null不能用= > <这些符号

  3、select product_name,sale_price,purchase_price from product where sale_price - purchase_price >= 500;

  3/2、select product_name,sale_price,purchase_price from product where sale_price >= purchase_price + 500;

  4、select product_name,product_type,sale_price*0.9 as "profit" from product where sale_price * 0.9 - purchase_price >= 100;

猜你喜欢

转载自www.cnblogs.com/jiegege/p/9164375.html