Fuzzy query of database

The higher the hit rate – the better the strategy

Fuzzy query of database work918

In SQL, fuzzy queries can be implemented using the LIKE keyword . The LIKE keyword can be followed by a pattern, where % represents any number of characters and _ represents one character.

For example, if you want to find all students whose names start with Li in a table named students, you can do this:

SELECT * FROM students WHERE name LIKE 'Li%';

If you want to find students whose names contain Li, you can do this:

SELECT * FROM students WHERE name LIKE '%Li%';

If you want to find students whose names are of length 3 and whose second character is i, you can do this:

SELECT * FROM students WHERE name LIKE '_i%';

The difference between % and _

In SQL, both % and _ are wildcard characters used for fuzzy queries, but their usage is different:

  1. - %: represents any number of characters (including 0 characters). For example, 'Li%' can match 'Li', 'Lily', 'Lion', etc.
  2. - : Indicates one character. For example, 'Li ' can match 'Lily', but not 'Li' or 'Lion'.

Here is an example to illustrate their difference:

SELECT * FROM students WHERE name LIKE 'Li%'; -- 匹配所有以'Li'开头的名字
SELECT * FROM students WHERE name LIKE 'Li_'; -- 只匹配名字为三个字符,且以'Li'开头的名字

Single table unconditional query format

Insert image description here

The DISTINCT keyword is used to return unique records. It removes duplicate rows from the result set.

For example, if you have a table called students with a column called major, you can use DISTINCT to find all the different majors:

SELECT DISTINCT major FROM students;

This will return a list where each major appears only once, even if there are multiple students with the same major in the students table.

Please note that the DISTINCT keyword applies to all columns. For example, if you select multiple columns, DISTINCT will return unique row combinations:

SELECT DISTINCT major, year FROM students;

This will return all unique major and year combinations. If two students have the same major and year, they will only appear once in the result set.

Example:

Insert image description here

How to write sorting

In SQL, you can use the ORDER BY keyword to sort query results. You can sort based on one or more columns, and can specify ascending (ASC) or descending (DESC) order.

For example, if you have a table called students, you can sort by the name column in ascending order:
SELECT * FROM students ORDER BY name ASC;
You can also sort by the name column in descending order:
SELECT * FROM students ORDER BY name DESC;
If you want to sort by multiple columns, you can list the columns after ORDER BY, separated by commas.

For example, you could sort by major first, and then sort by name within each major:

SELECT * FROM students ORDER BY major ASC, name ASC;

Example

Insert image description here

The database only needs the first five rows and the first five entered

SELECT * FROM table_name LIMIT 5;
或者(等价于)
SELECT * FROM table_name LIMIT 0,10;

Insert image description here

Then sort the points*2 output

Insert image description here

Points skyrocketed

method one:

(1)CASE

SELECT `sname`,`zy`,`jf`, 
   CASE 
     WHEN `zy` LIKE '%应用%' THEN `jf`+ 10 
     WHEN `zy` LIKE '%1%' THEN `jf` + 20 
      ELSE `jf`
     END AS updated_points 
FROM `stu_stu1`

(2)IF

SELECT `sname`, `zy`, `jf`,
       IF(`zy` LIKE '%应用%', `jf` + 10,
       IF(`zy` LIKE '%1%', `jf` + 20, `jf`)
       ) AS updated_points
FROM `stu_stu1`

Insert image description here

Method 2

SELECT `sname`,`zy`,`jf`, 
   CASE 
     WHEN `zy` LIKE '%应用%' THEN `jf`+ 10 
     WHEN `zy` LIKE '%1%' THEN `jf` + 20 
      ELSE `jf`
     END AS updated_points 
FROM(
    
    )AS sss
select '' AS ,'' AS ,FROMtable——name’ where zy =“名称”
SELECT `sname`,`zy`,`jf` FROM (SELECT `sname`, `zy`,`jf`
    FROM `stu_stu1`)AS sss WHERE `zy`='计算机应用'
select '' AS ,'' AS ,FROMtable——name’ where zy =“名称”
SELECT `sname`,`zy`,`jf` FROM (SELECT `sname`, `zy`,`jf`
    FROM `stu_stu1`)AS sss WHERE `zy`='计算机应用'

Guess you like

Origin blog.csdn.net/m0_74154295/article/details/133077183