简体   繁体   中英

How SQL selects between two strings/texts?

In SQL if I've the following select statement:

SELECT * FROM Products WHERE ProductName BETWEEN 'Geitost' AND 'Pavlova'

It run successfully and display the output records, but I'm a little bit confusing about how we can search between two strings and how the SQL perform it,

for example the following select statement:

SELECT * FROM Products WHERE Price BETWEEN 100 AND 300

It's clear that we are trying to find all the prices which has a price equals to 100$ or greater but less than or equals 300$

Can somebody explain what is the selecting mechanism between two strings in SQL?

Thanks in advance.

This is too long for a comment.

Your question is rather baffling. This code works:

WHERE Price BETWEEN 100 AND 300

It is turned into:

WHERE Price >= 100 AND Price <= 300

Similarly, your first condition is equivalent to:

WHERE ProductName >= 'Geitost' AND
      ProductName <= 'Pavlova'

So the question is about how strings are compared. The short answer is "how they appear in the dictionary". Of course, there are lots of dictionaries in the world. The more complete answer is based on collations , which describe the alphabetic ordering of strings.

Here is the MySQL documentation for collations and character sets. Although some details such as the names of the collations and the syntax for functions may vary, the idea is similar across databases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM