开发者

Matching first char in string to digit or non-standard character

I need 开发者_如何学运维to allow users to browse a table, with >1 million entries, by the first letter in the title.

I want them to be able to browse by every letter from A-Z, 0-9 in a list together and all other characters together.

Since it's a big database and it is to be displayed on a website, I need it to be efficient. Regex does not use index, so that would be too slow.

Is this possible or will I have to rethink the design?

Thanks in advance


As long as there's an index on the "Title", you should be able to use a SQL like

select * 
from myTable 
where Title like 'A%'
(or 'B%', 'C%'...)


Create links representing every letter and number. Clicking these links will provide the users with the results from the database that begin with the selected character.

SELECT title FROM table
WHERE LEFT(title,1) = ?Char
ORDER BY title ASC;

Consider paginating these result pages into appropriate chunks. MySQL will let you do this with LIMIT

This command will select the first 100 records from the desired character group:

SELECT title FROM table
WHERE LEFT(title,1) = ?Char
ORDER BY title ASC
LIMIT 0, 100;

This command will select the second 100 records from the desired character group:

SELECT title FROM table
WHERE LEFT(title,1) = ?Char
ORDER BY title ASC
LIMIT 100, 100;

Per your comments, if you want to combine characters 0-9 without using regex, you will need to combine several OR statements:

SELECT title FROM table
WHERE (
    LEFT(title,1) = '0'
    OR LEFT(title,1) = '1'
    ...
    )
ORDER BY title ASC;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜