初识psql(五)

初识psql (五) 之 where 字句

where子句的语法:

where search_condition

从定义上看我们知道,where后面跟的是一个条件搜索语句,即它的最后得到的返回值是布尔类型,boolean 只有两种可能truefalse,就是这个条件搜索语句执行之后,true就返回我们前面select后面的列;false 就是没有得到我们想要的数据

我们还是先定义一张表student

drop table if exists students;

create table if not exists students(
id integer not null,
name varchar(30) unique not null,
salary money not null,
create_at integer  
);

//创建自增主键
CREATE SEQUENCE students_id_seq
    INCREMENT 1
    START 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;
alter table students alter column id set default nextval('students_id_seq');

insert into students (name,saraly,create_at) values ('liyi',9996.32,1545837392),
('admin',12365.35,1545838392),
('root',2568.365,1545839392),
('test',1236548.25,1545537392);

表创建1
表创建1

表创建2

注意=>psql主键的创建语法:

CREATE [ TEMPORARY | TEMP ] SEQUENCE name [ INCREMENT [ BY ] increment ]
    [ MINVALUE minvalue | NO MINVALUE ] [ MAXVALUE maxvalue | NO MAXVALUE ]
    [ START [ WITH ] start ] [ CACHE cache ] [ [ NO ] CYCLE ]

-- 测试Sequences函数:
select nextval('students_id_seq');

测试Sequences函数3

  • where ... = 查询等于
select * from students where name = 'liyi';
  • where ... != 查询不等于
select * from students where salary != 9996.32;
  • where ... >|>= 查询大于或大于等于
select * from students where salary > 1000000;
select * from students where salary >= 12365.35;
  • where ... <|<= 查询小于等于
select * from students where salary < 10000;
select * from students where salary <= 12365.35;
  • where...in|not in 查询在 | 不在【…】某一区间
select * from students where name in ('admin','root');
select * from students where name not in ('admin','root');
  • where ... between|not between 查询在 | 不在【…】之间
select * from students where create_at between 1545537390 and 1545838392;
select * from students where create_at not between 1545537390 and 1545838392;
  • where ... like 模糊查询
select * from students where name likke '_iyi';
select * from students where name like '_iy_';
select * from students where name like '%yi';
select * from students where name like '%y%';
  • where ...in 后面我们接上select子句也是可以的哦;

我们之前创建过users表,这次我们也来使用一下

select * from students where name in (select username from users);

以上sql语句全不可以正常运行


学海无涯,跪在坚持,加油

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值