Data Filtering - Advanced search query data condition

```msyql

[---] AND operator
to more than one column by filtration, and the operator can use the additional where clause conditions,


select prod_id,prod_price,prod_name
from products
where vend_id=1003 and prod_price <=10;

[Analysis] This statement retrieves merchandise sql vendor number, and the price is less than or equal names and prices of all products of 10, this select statement that contains the where clause to the condition, and a link to their use and keywords,
and instructions DBMS only returns all rows that satisfy a given condition. If a commodity supplier, manufacturing 1003, the price will not be higher than 10 to retrieve it.

Test result will be:

prod_id    |     prod_price    |    prod_name

fb                 |    2                    |    sangsing

hb                |    2                    |    sa
opb              |    5                    |    sa
kkb              |    2.3                   |    sang

and using the keywords in the where clause to indicate retrieval satisfy all the conditions given column

And using more filters may be added, and is added in the back of a a

--- OR operator []

and the or operator and the different operator, it indicates any of the conditions matching MySQL retrieves rows
as select statement

select prod_name,prod_price
from products
where vend_id =1002 or vend_id=1006;

time or retrieved, but a search of them, as long as there will be a set up check out the results, if not, then the query is no data
and is a must for all conditions to be established before they can, so the use and or or according to the actual situation to use.

- [order] is calculated
where may comprise any number of and and or operators, allowing complex combination of both has been advanced by filtration
using MySQL query statement following data:
If you need to list the prices of not less than 10 and 1002 or 1003 All products manufactured using a combination of the following select statement and and or operators of the establishment of a data query where clause

select prod_name,prod_price
from products
where vend_id =1002 or vend_id =1003 and prod_pricce >=10;

查询结果是:
prod_name    |    prod_price

bird                  |    13
bold                 |    14
ayrd                  |    8
awrd                  |    5
oldd                  |    33

By querying the above results, we can see the results of the query there are two data is less than 10, it is because of what?
! ! ! The reason is that the order of the computer.
[Prior to treatment or operator, and the operator will be priority, when the where clause sql see above, it is understood that any price 1003 manufactured by vendors 10 or more products, or manufactured by a vendor 1002 any product, regardless of price,

In other words, since the AND highest priority in the computer, the operator is erroneously combined. ]

Solution to this problem is the use of parentheses corresponds to a packet enclosed.


select prod_name,prod_price
from products
where (vend_id =1002 or vend_id =1003) and prod_pricce >=10;


查询结果是:
prod_name    |    prod_price

bird                  |    13
bold                 |    14
oldd                  |    33

In the where clause in parentheses, and any time or using a where clause or operation, should use explicit parentheses to group operators, do not over-rely on the default computer order, even if it is what you want The same is true, use parentheses, no harm, it can eliminate ambiguity.

--- operator [in]

In parentheses in the where clause there is another use, the operator specifies the conditions in range, in the range of each condition can be matched, in a comma-separated list of legal values ​​taken, all enclosed in parentheses in.


select prod_name,prod_price
from products
where vend_id in(1002,1003)
order by prod_name;

查询结果是:
prod_name    |    prod_price

bird                  |    13
bold                 |    14
ayrd                  |    8
awrd                  |    5
oldd                  |    33

in a legitimate representative of the range there, as long as the values ​​are legal in this interval inside, so if you want to take on the value of the interval in the legal syntax to use value.

in operator and set without operator or perform the same function.
--- [use in] the advantage of operators
- in the use of legitimate long list of options, in grammar operators a clearer and more intuitive
- when in use, the order of calculation easier to manage, because the operators used more less
-in operator or operator than is generally performed more quickly and efficiently Listing
-in biggest advantage is the select statement can contain other, making it possible to establish a more dynamic where clause! ! !

- operators [not]
where clause not operator, and only one function, that is to deny its condition after the heel.
--not where clause to deny keyword followed by the condition.
To prove it is not in the product ID 1002 and 1003, write the following code:


select prod_name,prod_price
from products
where vend_id not in (1002,1003)
order by prod_name;

prod_name    |    prod_price

d                  |    13
ld                 |    14
rd                  |    8
wrd                  |    5
ldd                  |    33

not advantages in complex high-level inquiry will be useful, for example, when used in combination and in the operator, not the line to find out the list of conditions that do not match very simple.

```

Guess you like

Origin www.cnblogs.com/ludundun/p/11612195.html