postgresql (PG) 字段用逗号 “,”隔开 判断是否含有某个值

时间:2017-08-09 15:39:30   收藏:0   阅读:4086

如有一个student 学生表

student表中有字段 课程字段 分别用 1,2,3,4,5,6,7 表示不通的7门课程

CREATE TABLE student

(

   name varchar(255),

  course varchar(255)

)

insert into student (name ,course) 

values (‘张三‘,‘1,2,5,7‘);


问题一、判断 张三 是否选择了 课程 2

select * from 

where name = ‘张三‘

and string_to_array(course,‘,‘) @> array[‘2‘]


问题二、判断张三是否同时选择了课程2,6

select * from 

where name = ‘张三‘

and string_to_array(course,‘,‘) @> array[‘2‘,‘6‘]


本文出自 “简单可依赖” 博客,请务必保留此出处http://wenxuehui.blog.51cto.com/12898974/1954697

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!