How do I select by a range of starting characters?

2019-02-26 08:48发布

Here is my mySQL query:

SELECT *
FROM `eodList` 
WHERE datechanged>='$curdate' 
GROUP BY symbolName 
ORDER BY dateChanged DESC

How do I get all rows (for example) where symbolName starts with letters A-F?


Update:

I need something that can be easily changed to another range without alot of code changes.

3条回答
beautiful°
2楼-- · 2019-02-26 09:28

Faster than Regular expressions and SUBSTRING() function calls. This will use the index of symbolName :

WHERE symbolName >= 'A' 
  AND symbolName < 'G'

There is an issue with the case sensitivity though. Do you want names which start with a..f too or not?

If you want only names that start with uppercase and the table has utf8 character set, use:

WHERE symbolName >= 'A' COLLATE utf8_bin
  AND symbolName < 'G' COLLATE utf8_bin

For other character sets, use the corresponding _bin collation.

查看更多
Fickle 薄情
3楼-- · 2019-02-26 09:33

Try with Regex

WHERE symbolName REGEXP '^[A-F]'
查看更多
地球回转人心会变
4楼-- · 2019-02-26 09:34
SELECT *
FROM `eodList` 
WHERE datechanged>='$curdate' AND


((SUBSTRING(symbolName, 1, 1) >= "A" AND SUBSTRING(symbolName, 1, 1) <="F"))
GROUP BY symbolName 
ORDER BY dateChanged DESC
查看更多
登录 后发表回答