Jump to content

[SOLVED] Selecting by letter and non-alphabetical letter


Recommended Posts

I have a table of data that I'm trying to fetch out alphabetically from. Sort of like a A-Z list where you click a letter and results starting with that letter are shown.

 

So I have:

SELECT * FROM table WHERE column LIKE 'a%'

for items that start with a.

My first question is that is there any difference from using

SELECT * FROM table WHERE SUBSTRING(column,1,1) = 'a'

I checked the explain and they both looked the same. But it seems like substring is faster? Does one use less resources or is faster?

 

My second question is how can I fetch results that don't start with a-z. The closest I've gotten is:

SELECT * FROM table WHERE column NOT BETWEEN 'A' AND 'Z'

but that will also show results starting with Z. So if I do

SELECT * FROM table WHERE column NOT BETWEEN 'A' AND 'Z' AND column NOT LIKE 'Z%'

it'll work, but I'm sure there would be a better way to do it?

 

Thanks.

You could use this for matching non alphabets

SELECT nick FROM users WHERE nick NOT REGEXP '^[a-z]'

Thanks, that works great. Didn't know you could use regexp with mysql.

 

Anyone have a suggestion for using LIKE vs SUBSTRING? Or would use either one be fine?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.