【数据库】查询:WHERE、算术运算符、条件运算符

news/2024/7/9 21:26:21 标签: sql, postgresql, mysql, 数据库

程序加载语句:

sql">C:\Users\49161>E:\sql1\bin\psql.exe -U postgres

输出表里的两列内容:

sql">postgres=# SELECT product_id,product_name
postgres-# FROM Product;

查询表里的所有列:*

sql">postgres=# SELECT *
postgres-# FROM Product;

格式:
空行:不可以
换行:可以

设定别名:

sql">postgres=# SELECT product_id AS id,
postgres-# product_name AS name,
postgres-# purchase_price AS price
postgres-# FROM Product
postgres-# ;

设定成中文:

sql">postgres=# SELECT product_id AS "商品编号",
postgres-# product_name AS "商品名称",
postgres-# purchase_price AS "进货单价"
postgres-# FROM Product;
 商品编号 | 商品名称 | 进货单价
----------+----------+----------
 0001     | T恤衫    |      500
 0002     | 打孔器   |      320
 0003     | 运动T恤  |     2800
 0004     | 菜刀     |     2800
 0005     | 高压锅   |     5000
 0006     | 叉子     |
 0007     | 擦菜板   |      790
 0008     | 圆珠笔   |
(8 行记录)

删除重复行:

sql">postgres=# SELECT DISTINCT product_type
postgres-# FROM Product;
 product_type
--------------
 衣服
 办公用品
 厨房用具
(3 行记录)

对NULL数据同样有效
删除多列时,先将多列合并成一组数据,再执行删除操作

sql">postgres=# SELECT DISTINCT product_type,regist_date
postgres-# FROM Product
postgres-# ;
 product_type | regist_date
--------------+-------------
 办公用品     | 2009-09-20
 厨房用具     | 2009-01-15
 办公用品     | 2009-11-11
 衣服         |
 厨房用具     | 2008-04-28
 厨房用具     | 2009-09-20
 衣服         | 2009-09-20
(7 行记录)

WHERE 条件表达式做筛选

sql">postgres=# SELECT product_name,product_type
postgres-# FROM Product
postgres-# WHERE product_type='衣服';
 product_name | product_type
--------------+--------------
 T恤衫        | 衣服
 运动T恤      | 衣服
(2 行记录)

也可以提取不作为查询条件的列:

sql">postgres=# SELECT product_name
postgres-# FROM Product
postgres-# WHERE product_type='衣服';
 product_name
--------------
 T恤衫
 运动T恤
(2 行记录)

注意事项:
书写顺序,where写在后面

注释:
单行:–
多行:/* */

2.17计算表达式:

sql">postgres=# SELECT product_name,sale_price,
postgres-# sale_price*2 AS "sale_price_*2"
postgres-# FROM Product;
 product_name | sale_price | sale_price_*2
--------------+------------+---------------
 T恤衫        |       1000 |          2000
 打孔器       |        500 |          1000
 运动T恤      |       4000 |          8000
 菜刀         |       3000 |          6000
 高压锅       |       6800 |         13600
 叉子         |        500 |          1000
 擦菜板       |        880 |          1760
 圆珠笔       |        100 |           200
(8 行记录)

注意:
带NULL的运算结果都是NULL

2.18选取售价为500的记录:

sql">postgres=# SELECT product_name,product_type
postgres-# FROM Product
postgres-# WHERE sale_price=500;
 product_name | product_type
--------------+--------------
 打孔器       | 办公用品
 叉子         | 厨房用具
(2 行记录)

2.19选售价不为500的记录:

sql">postgres=# SELECT product_name,product_type
postgres-# FROM Product
postgres-# WHERE sale_price<>500;
 product_name | product_type
--------------+--------------
 T恤衫        | 衣服
 运动T恤      | 衣服
 菜刀         | 厨房用具
 高压锅       | 厨房用具
 擦菜板       | 厨房用具
 圆珠笔       | 办公用品
(6 行记录)

2.21
日期也可以用比较运算符:
在2009.09.27之前的商品

sql">postgres=# SELECT product_name,product_type,regist_date
postgres-# FROM Product
postgres-# WHERE regist_date<'2009-09-27';
 product_name | product_type | regist_date
--------------+--------------+-------------
 T恤衫        | 衣服         | 2009-09-20
 打孔器       | 办公用品     | 2009-09-20
 菜刀         | 厨房用具     | 2009-09-20
 高压锅       | 厨房用具     | 2009-01-15
 叉子         | 厨房用具     | 2009-09-20
 擦菜板       | 厨房用具     | 2008-04-28
(6 行记录)

2.22WHERE语句也可以用条件表达式:

sql">postgres=# SELECT product_name,sale_price,purchase_price
postgres-# FROM Product
postgres-# WHERE sale_price -purchase_price>500;
 product_name | sale_price | purchase_price
--------------+------------+----------------
 运动T恤      |       4000 |           2800
 高压锅       |       6800 |           5000
(2 行记录)

2.24字符串表中选择大于’2‘的数据:

sql">postgres=# BEGIN TRANSACTION;
BEGIN
postgres=# INSERT INTO Chars VALUES('1');
INSERT 0 1
postgres=# INSERT INTO Chars VALUES('2');
INSERT 0 1
postgres=# INSERT INTO Chars VALUES('3');
INSERT 0 1
postgres=# INSERT INTO Chars VALUES('11');
INSERT 0 1
postgres=# INSERT INTO Chars VALUES('12');
INSERT 0 1
postgres=# INSERT INTO Chars VALUES('222');
INSERT 0 1
postgres=# COMMIT;
COMMIT
postgres=# SELECT chr
postgres-# FROM Chars
postgres-# WHERE chr>'2';
 chr
-----
 3
 222
(2 行记录)

2.28选取NULL记录:

sql">postgres=# SELECT product_name,purchase_price
postgres-# FROM Product
postgres-# WHERE purchase_price IS NULL;
 product_name | purchase_price
--------------+----------------
 叉子         |
 圆珠笔       |
(2 行记录)

2.29选取不为NULL的记录:

sql">postgres=# SELECT product_name,purchase_price
postgres-# FROM Product
postgres-# WHERE purchase_price IS NOT NULL;
 product_name | purchase_price
--------------+----------------
 T恤衫        |            500
 打孔器       |            320
 运动T恤      |           2800
 菜刀         |           2800
 高压锅       |           5000
 擦菜板       |            790
(6 行记录)

注意:
1.比较运算符,相等,用单等号
不相等:<>
2.比较运算符不能判断是否为NULL
3.判断NULL,用IS NULL语句
4.字符串是按照字典顺序排序的:
1 11 12 2 222 3

逻辑运算符:
NOT表否定
AND并
OR或
2.33AND语句

sql">postgres=# SELECT product_name,purchase_price
postgres-# FROM Product
postgres-# WHERE product_type='厨房用具'
postgres-# AND sale_price>=3000;
 product_name | purchase_price
--------------+----------------
 菜刀         |           2800
 高压锅       |           5000
(2 行记录)

2.34OR语句

sql">postgres=# SELECT product_name,purchase_price
postgres-# FROM Product
postgres-# WHERE product_type='厨房用具'
postgres-# OR sale_price >=3000;
 product_name | purchase_price
--------------+----------------
 运动T恤      |           2800
 菜刀         |           2800
 高压锅       |           5000
 叉子         |
 擦菜板       |            790
(5 行记录)

2.36括号的优先级最高:

sql">postgres=# SELECT product_name,purchase_price
postgres-# FROM Product
postgres-# WHERE product_type='办公用品'
postgres-# AND (regist_date='2009-09-11'
postgres(# OR regist_date='2009-09-20');
 product_name | purchase_price
--------------+----------------
 打孔器       |            320
(1 行记录)

注意:AND优先级高于OR

sql是三值逻辑,还有UNKNOWN不确定

练习2.4

sql">postgres=# SELECT product_name,product_type,
postgres-# sale_price*9/10-purchase_price AS profit
postgres-# FROM Product
postgres-# WHERE sale_price*0.9-purchase_price >100
postgres-# AND (product_type='办公用品')
postgres-# ;
 product_name | product_type | profit
--------------+--------------+--------
 打孔器       | 办公用品     |    130
(1 行记录)

and和or的书写格式;新建列名;条件判断不用列名


http://www.niftyadmin.cn/n/1757262.html

相关文章

Python之xlrd模块使用

python处理Excel 相关说明&#xff1a; 1、Python自带的csv模块可以处理.csv文件。 2、xlrd和xlwt两个模块分别用来读Excel和写Excel&#xff0c;只支持.xls和.xlsx格式&#xff0c;xlutils模块可以同时读写一个已存在的Excel文件&#xff0c;依赖于xlrd和xlwt。 3、openpyxl…

【数据库】聚合和排序

聚合函数&#xff1a;用于汇总的函数 COUNT&#xff1a;计数 SUM AVG MIN MAX 3.1计算全部行数&#xff1a; postgres# SELECT COUNT(*) postgres-# FROM Product;count -------8 (1 行记录)3.2COUNT某列不计入NULL值&#xff0c;COUNT&#xff08;*&#xff09;会计入 post…

SQL基本用法总括一

整理一份SQL基本用法&#xff0c; 包含&#xff1a;①表&#xff1a; 创建&#xff0c;查询&#xff0c;修改&#xff0c;重命名 ②删除&#xff1a;字段&#xff0c;表&#xff0c;数据&#xff0c;主键&#xff0c;自增长 ③新增&#xff1a;字段&#xff0c;表 ④更新&…

【java笔记】final关键字、权限修饰符、内部类

/* final关键字&#xff0c;表示最终的&#xff0c;不可变的 可以修饰&#xff1a; 1.类 2.方法 3.成员变量 4.局部变量 */ final修饰一个类&#xff1a; package demo01; /* final修饰一个类&#xff0c;不能有任何子类&#xff08;俗称太监类&#xff09; final里的方法不可…

SQL基本用法总括二

SQL基本用法总括第二篇&#xff0c;能稍微高级一点点&#xff0c;但是也很常用&#xff0c; 包含&#xff1a;①过滤 ②通配符 ③计算字段 ④函数、子查询、组合查询 ⑤视图 ⑥存储过程 ⑦游标 ⑧触发器、事务管理 ⑨字符集 ⑩权限管理 详细如下&#xff1a; 一、过滤 wher…

【Java】常用的API:Math,System,Object,Arrays

1.Math 2.System package demo01;public class MySystem {public static void main(String[] args) {/*System.out.println("Start");System.exit(0);//终止当前运行的java虚拟机System.out.println("End");*/System.out.println(System.currentTimeMilli…

细聊 SELECT COUNT 用法

数据库查询相信很多人都不陌生&#xff0c;所有经常有人调侃程序员就是CRUD专员&#xff0c;这所谓的CRUD指的就是数据库的增删改查。 在数据库的增删改查操作中&#xff0c;使用最频繁的就是查询操作。而在所有查询操作中&#xff0c;统计数量操作更是经常被用到。 关于数据…

图解 Python 函数

函数是 “ 一系列命令的集合”&#xff0c;我们可以通过调用函数来自动执行某一系列命令。虽然经常性地出现于文章中的print()是被录入在Python的标准库中的函数&#xff0c;但是&#xff0c;程序员亦可创建自己的函数。 如果想要定义函数&#xff0c;则需要以“def 函数名():…