Application of SELECT query

Source: Internet
Author: User

For example, we need to find all the articles containing the word "MySQL" from the titles of many articles. This should be used in the WHERE clause "LIKE", that is, fuzzy query.
 
First, let's talk about the unified character in the SQL language. The unified character is to match any character in a single way. In SQL, a character "_" matches any character in a single character; one character "%" matches any zero to multiple characters. For example, "A _" can match "AA", "AB", "A2", and "A $ "...... "A %" matches "ABCD", "A", "AG $ Bng0", and "An apple is just an apple ."...... And so on.
 
How to use it? Let's take A practical example: there are A lot of things in the factory warehouse and they are very complicated. They should be managed in several categories: for example, Class A represents Machine Accessories and Class B represents tools, class C represents packaging materials ...... And so on. The numbers in the following categories are numbered separately, such as "A0001", "A1065", and "B1014. When you manage this database, the number is used as a field. This field is used not only as the item code, but also as its category. When you want to query all the tools, you can:
SELECT * FROM goods WHERE code LIKE 'B % 'order BY Code'
In this way, all records whose names start with the letter B are queried. Note that 'B %' indicates that the letter B appears at the beginning of the field. Different from the next example, you can find all the articles in the article title that contain the word "MySQL, because the target MySQL does not determine the position of the article title, you should:
SELECT * FROM articles WHERE title LIKE '% MySQL %'
'% MySQL %' indicates that the word "MySQL" appears in the title. It may be preceded by text or followed by text.
 
Since MySQL can be queried, it is not recommended to query other words. This allows a viewer to enter the keyword he/she wants to search:
<! -- Input. php: -->
<Form method = "get" action = "search. php">
Keywords: <input type = "text" size = 10 name = "key"> <input type = "submit" value = "query">
</Form>
<! -- Search. php: -->
<?
...
$ Query = "select * from article where title like '% $ key % '";
$ Result = $ mysql_query ($ query, $ db );
...
?>
Note that when using fuzzy search, you should be careful when a field is a value similar to '2, 13, 25, 33, if you want to query whether it contains '2', you cannot simply query it using '% 100. Because in this case, a value like this will also be queried: '1, 6, 21, 27 '. This is not what we want. Therefore, the field should be saved as '. 2.13.25.33. In this way, you can use '%. 2. %' with confidence to query records containing '2.
 

A rush of time. please correct me if there is any inaccuracy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.