Jump to content

limit mysql output by letter


nuttycoder

Recommended Posts

Hi I am building a drinks database/search site.

 

At present I have 26 tables 1 table for each letter like drinksa, drinkb, drinkc..ect which represent a, b, c...ect

 

I have realised this methed it not good practice as I can't search all 26 tables in one go.

 

What I would like to do now is put all the drink a to z in one table called drinks.

 

I would then be able to search that 1 table, this is fine I can do that without a problem.

 

But I am haing some difficulty getting my head around the front end what I mean is on the front end of the site on the drinks page there are links to each section like a, b ,c, d, e,......x, y, z.

 

But if all the data is in one table then all the drinks a to z would be output I know I can limit how many to output but I was wondering is it possible to limit by starting letter

 

For example when a user clicks "A" then all the drink starting with "A" are output but not any other drinks starting with a diffrent letter.

 

This would be done by usin an sql statment I belive by using limit but am not sure if its possble to limit by letter.

 

I have been looking at a lot of foums and so far I have not come across anything that may be of help so I though I'd ask

 

If anyone would point me in the rihgt direction or could expmain if this is possible or a better way of doing this I would be very greatful

 

Thanks

 

Dave

Link to comment
https://forums.phpfreaks.com/topic/52937-limit-mysql-output-by-letter/
Share on other sites

There's a 2x performance advantage by using LIKE instead of LEFT.  Nearly insignificant, but I was doing this benchmark this morning anyway, and I thought I'd share it.

 

mysql> SELECT BENCHMARK(1000000,LEFT('Aaaaaaa',1)='A');
+------------------------------------------+
| BENCHMARK(1000000,LEFT('Aaaaaaa',1)='A') |
+------------------------------------------+
|                                        0 | 
+------------------------------------------+
1 row in set (0.14 sec)

mysql> SELECT BENCHMARK(1000000,'Aaaaaaa' LIKE 'A%');
+----------------------------------------+
| BENCHMARK(1000000,'Aaaaaaa' LIKE 'A%') |
+----------------------------------------+
|                                      0 | 
+----------------------------------------+
1 row in set (0.07 sec)

 

 

(Just don't say the word "drinks" this morning.  Ugghh....)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.