Application of SELECT query

Source: Internet
Author: User
For example, we'll find all the articles that contain the word "MySQL" from the titles of many articles. This should be used in the WHERE clause "like", that is, fuzzy query.
 
First of all, to explain the wildcard in SQL language, wildcard is a character to match any character uniformly, in SQL, a character "_" matches any single character, and a character "%" matches any 0 to many characters. For example, "A_" can match things like "AA", "AB", "A2", "a$" ... And so on, and "a%" can Match "ABCD", "A", "AG$BNG0", "an apple are just an apple." ...... Wait a minute.
 
How to use it? Or to give a practical example: The Factory warehouse Dongdong, many, very miscellaneous, to be divided into several major categories to manage: For example, a class representative machine accessories, B-Class representative tools, C-Class representative packaging materials ... And so on, each category below the east and then separately numbered, such as "A0001", "A1065", "B1014" and so on. In managing this database, the number is used as a field, not only as a code for the item, but also as a description of its category. When you want to query all the tools, you can do this:
SELECT * FROM goods WHERE code like ' b% '
This is the query code this field (that is, numbering) is the beginning of the letter B all records. Notice here ' b% ' means that the letter B is at the beginning of the field, unlike the next example: Find all articles in the title of the article that contain the word "MySQL," because the target "MySQL" to look for does not necessarily appear in the title of the article, so you should:
SELECT * from articles WHERE title like '%mysql% '
'%mysql% ' means that the word "MySQL" appears in the title, which may have text in front of it, and may be followed by text.
 
Since the search "MySQL" can be, look for other words may not. This allows the viewer to enter the keyword he is searching for himself:
<!--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 a fuzzy query, one thing you should be careful about is that when a field is a value like ' 2,13,25,33 ', to query whether it contains the item ' 2 ', you can't simply query it with '%2% '. Because if so, one of these values will also be queried: ' 1,6,21,27 '. That's not what we want. So when dealing with such a problem, you should Gencun the word ". 2.13.25.33." So when you need to query the records containing ' 2 ', you can be assured of using '%.2.% '.
 

Time is short, make a draft;

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.