MySQL_011_where条件,and和or的用法,not,is not null,in ,bewteen,like

表名为没有mytable, 如下图

MySQL_011_where条件,and和or的用法,not,is not null,in ,bewteen,like_第1张图片

数学符号:大于>  小于<  等于=   不等于<>、!=

字符串:大于>  小于<  等于=   不等于<>、!=

查看id不等于1的数据:输入:select * from mytable where id !=1; 如下图

MySQL_011_where条件,and和or的用法,not,is not null,in ,bewteen,like_第2张图片

查看username等于 牛是方的 这条数据,输入:select * from mytable where username='牛是方的';   如下图

MySQL_011_where条件,and和or的用法,not,is not null,in ,bewteen,like_第3张图片

查看id等于2和username等于 牛是芳的 这条数据,输入select * from mytable id=2 and username='牛是芳的';

MySQL_011_where条件,and和or的用法,not,is not null,in ,bewteen,like_第4张图片

查看id等于1或username等于 牛是芳的 这条数据,输入:select * from mytable where id=1 or username='牛是芳的'; 如下图,把满足这俩个的条件都显示出来了

MySQL_011_where条件,and和or的用法,not,is not null,in ,bewteen,like_第5张图片


查看id=1或username='牛是芳的'和password='123456789'这条数据,输入:select * from mytable where id=1 or username='牛是芳的' and password ='123456789';  

 如下图,解读:因为and的优先级高,所以满足的是(id=1) or (username='牛是芳的' and password ='123456789')

MySQL_011_where条件,and和or的用法,not,is not null,in ,bewteen,like_第6张图片


再看这条,输入:select * from mytable where (id=1 or username='牛是芳的') and (password ='123456789'); 这个语句什么也不会显示,因为前面的id=1或username='牛是芳的' 这俩个条件要同时满则password='123456789';,满足不了,所以不会输出


再看这条,输入:select * from mytable where (id=3 or username='牛是芳的') and password ='123456789';   如下图

MySQL_011_where条件,and和or的用法,not,is not null,in ,bewteen,like_第7张图片


使用not查看id不等于1的数据,输入:select * from mytable where not id=1;   如下图

MySQL_011_where条件,and和or的用法,not,is not null,in ,bewteen,like_第8张图片


is 只能判断是null 或者 not null,如下图


使用in,查看满足id=1和2的数据,输入:select * from mytable where id in(1,2);   如下图,只要满足id=1和id=2的都会显示出来

MySQL_011_where条件,and和or的用法,not,is not null,in ,bewteen,like_第9张图片


使用in,查看满足id=3和4的数据,输入:select * from mytable where id in(3,4);   如下图,因为没有id=4的数据,所以只输出了id=3的

MySQL_011_where条件,and和or的用法,not,is not null,in ,bewteen,like_第10张图片


between and的用法

原表如下图

MySQL_011_where条件,and和or的用法,not,is not null,in ,bewteen,like_第11张图片

between and的用法,输入:select * from mytable where id between 2 and 4;   如下图,只查看了id2到4的数据,

还有not between x and y;的用法

MySQL_011_where条件,and和or的用法,not,is not null,in ,bewteen,like_第12张图片

like,第一种模糊查询username中以 牛 开头的数据:输入:select * from mytable username like '牛%';   如下图

 MySQL_011_where条件,and和or的用法,not,is not null,in ,bewteen,like_第13张图片

like,第二种,查询username以a开头后面确定有几个字符的数据,

输入:select * from mytable where username like 'a________';   如下图,a后面有8个下划线,表示要查询以a开头的一共9个字符的username

MySQL_011_where条件,and和or的用法,not,is not null,in ,bewteen,like_第14张图片

like,第三种,查询只要username中有b这个字符的数据,输入:select * from mytable where username like '%b%';  

如下图,查询的就是只要字符中有b就行,

MySQL_011_where条件,and和or的用法,not,is not null,in ,bewteen,like_第15张图片





你可能感兴趣的:(MySQL学习)