[mysql learning] 6.where advanced data filtering

       In this article, we will learn other uses of where, various operators, and use where to filter the retrieved data to get the data we want to query. 


content

Ready to work

WHERE advanced query

AND operation

OR operator

Mixing OR and AND

IN operator 

NOT operator

Summarize 


Ready to work

        The table we use this time is the same as in the previous article. The statement to create the table is as follows:

create table product(id int primary key auto_increment,
		supplier varchar(32),
		name varchar(32) not null,
		price float);
insert into product values(null,'中国','显示屏',800);
insert into product values(null,'美国','芯片',2000);
insert into product values(null,'美国','电池',92);
insert into product values(null,'中国','后盖',60);
insert into product values(null,'中国','PCB',550);
insert into product values(null,'日本','摄像头',285);
insert into product values(null,'新加坡','图像传感器',333);
insert into product values(null,'俄罗斯','马达',160);
insert into product values(null,null,'外壳',60);

        After inserting the data, check whether the data is added successfully, and enter  select * from product;  if it is displayed as follows, then our preparations are completed.


WHERE advanced query

        We have learned the where statement before, now let's learn about other uses of the where statement. We learn where is divided into two articles, this article is where advanced.


AND operation

        We can use the and operator to connect the conditions behind the where. The meaning of and is the meaning of sum . If there are two conditions behind where, then both conditions must be satisfied.

        We use the and operator to query products whose supplier is China and whose price is greater than 500

select * from product where supplier = '中国';

         The above statement is to query the data that the supplier is China. Below we use and to display the data whose price is greater than 200

 select * from product where supplier = '中国' and price > 200;

        Note: There is no limit on the number after the and operator, and multiple ands can be used. 


OR operator

        The usage of the OR operator is the same as that of the AND operator, except that or means or . If there are two conditions behind where to use or to connect, then one of these two conditions can be satisfied.

        We use the or operator to query data where the supplier is China or the price is greater than 1000

select * from product where supplier = '中国' or price > 1000;

        Note: There is no limit on the number of or operators, and multiple ors can be used. 


Mixing OR and AND

        OR and AND can be mixed, that is, you can use either or or and after where, but they have a priority. If you have learned programming languages, you should know that and has a higher priority than or, and the same is true here.

select * from product where supplier = '中国' and price > 200 or id=3;
 select * from product where supplier = '中国' or price > 200 and id=3;

        You can think about the meaning of the above two statements, and the following is the query result, you can see if it is the same as what you think.

         We can see that the priority of and is indeed higher than that of or. Here we only have and and or or after where. What if there are multiple ands and ors? This is not easy to understand, so we use () to increase the priority , and we use () to wrap the data we want to execute first .

        Next we use () to let or execute before and.

 select * from product where (supplier = '中国' or price > 200) and id > 3;

        Note: When we use OR and AND, try to use parentheses to specify the order of execution. 


IN operator 

        Let's not talk about how to use the IN operator, let's use the sql statement to query the data of id=1, id=3, id=5.

select * from product where id = 1 or id = 3 or id = 5;

         What we wrote above does implement the function, but it does not feel particularly troublesome. At this time, when we retrieve multiple values ​​at the same time, we can use the IN operator.

        Column name in(value1, value2, value3,...), this is the usage of in, which means to query all data whose column name satisfies the value in in. We use in to achieve the above function

select * from product where id in(1, 3, 5);

         Writing our sql in this way introduces a lot, is clearer, and more intuitive. We use IN there are many advantages, such as: IN is faster than OR execution, can include other select statements, can dynamically create where clauses, etc. ( we will explain these in the following articles ).


NOT operator

         This operator is very simple, which is to negate our conditions. Next, we use the not operator to query all data whose suppliers are not China.

 select * from product where not supplier = '中国';

         Note that when we use not, NULL data will be automatically filtered in the result. One of the data suppliers in our data above is NULL. At this time, if we use NOT query, then NULL will be automatically filtered.


Summarize 

        We introduced the ADN, OR, IN, NOT operators above, doesn't it feel very simple? In fact, these are not simple, especially when they are mixed, we will integrate the basic operators later, and the difficulty will be reflected in the multi-table joint query. You are also welcome to visit my later articles.

Guess you like

Origin blog.csdn.net/m0_51545690/article/details/123620474