View Full Version : Searching Mysql Database for Alphabet Listing?
goldensea80
Aug 22nd 2005, 5:29 am
Yes, I dealing with this. Do you have idea? Links to articles?...
Thanks a lot!
vectorgraphx
Aug 22nd 2005, 5:54 am
Might be a bit more specific... what exactly are you trying to do?
select a group of records where field value begins with "A" or "B" etc... depending on user input?
if that's the case, use the % wildcard in your where clause.
sql = "SELECT * from yourtable WHERE yourfield LIKE '" & user_input_variable &"%'"
(notice, the wildcard is after the user input variable, not before. if you want to match any part of the field, put the wildcard both before and after the user variable.)
If you're just wanting to sort alpha, that part is much easier. Just use the ORDER clause with either asc (ascending) or desc (descending). if it is an alpha field, it will assume the ascending order is alpha.
sql = "SELECT * from yourtable ORDER by yourfield ASC"
Otherwise, we'll need more info if these examples don't fit your needs.
VG
goldensea80
Aug 22nd 2005, 6:57 am
Thanks,
Here is what I did and it work nicely so far:
$where="song_name REGEXP '^[$keywords]'";
$sql="SELECT * FROM tb_songs
WHERE $where
";
To check if a song associate with a keywords exists
function is_song_abc($keywords)
{
global $_CONNECTION;
$where="song_name REGEXP '^[$keywords]'";
$sql ='SELECT count(*) as total
FROM '.TB_SONGS."
WHERE $where";
$result = sql_query($sql);
return mysql_result($result,'total');
}
See the attachment!
av1
Aug 27th 2005, 10:48 am
try this: select * from db where name like ('a%');
takes only those records that start with letter a
goldensea80
Aug 28th 2005, 5:15 am
try this: select * from db where name like ('a%');
takes only those records that start with letter a
That's right.
But in case of the records that start with numbers (0-9), that won't works. But the method with REGEXP works. You can check my block for more detail
av1
Aug 28th 2005, 9:48 am
hmm, for numbers i use select * from db where name like '0%' or name like '1%' or... u get the idea.
i wonder which is faster, ur regexp thing or my 10 or's
Gmorkster
Aug 28th 2005, 12:51 pm
REGEXP '^[a-z0-9]' ?
av1
Aug 28th 2005, 12:58 pm
dude, u totally missed the point, the guy wants to get a list of records starting with numbers, so it would be '^[0-9]'
goldensea80
Aug 28th 2005, 7:11 pm
dude, u totally missed the point, the guy wants to get a list of records starting with numbers, so it would be '^[0-9]'
That's right. I don't know what will be faster (Maybe I'll test it infuture). But the REGEXP code will be shorter for both number and character. You don't have to change the search code, and the keywords array will be: '0-9','a',...,'z'
Gmorkster
Aug 28th 2005, 11:11 pm
Guess I did miss the point, heh
Run a test with ab (it's in the Apache bin dir by default) to see which one's faster
goldensea80
Aug 29th 2005, 4:35 am
* For REGEXP '^a'
1000 queries took: 0.48331189155579 second
For LIKE 'a%'
1000 queries took: 0.35055303573608
So that "LIKE" method is faster.
* For Fields started with Numbers
song_name REGEXP '^[0-9]'
1000 queries took: 0.48750901222229 seconds
song_name LIKE '0%' OR song_name LIKE '1%' OR song_name LIKE '2%' OR song_name LIKE '3%' OR song_name LIKE '4%' OR song_name LIKE '5%' OR song_name LIKE '6%' OR song_name LIKE '7%' OR song_name LIKE '8%' OR song_name LIKE '9%'
1000 queries took: 0.5047070980072 seconds
So that "REGEXP" method is faster.
av1
Aug 29th 2005, 11:03 am
thnx for the numbers :)
Gmorkster
Aug 29th 2005, 12:38 pm
and this is how it should be, the REGEXP involves one (resource consuming) operation while the 10 LIKE's are 10 operations logically ORed
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.