Forum Moderators: coopster

Message Too Old, No Replies

Selecting only certain items in MYSQL by first letter

Mysql First letter search

         

istojanovic

11:48 am on Jan 24, 2009 (gmt 0)

10+ Year Member



gregtheorangeman says:
Hi, I am making a review site for a friend, its a movie review website, im using php and mysql (phpmyadmin) and I ran into a problem. I am able to submit, edit, and display reviews, but I have a menu system i want to work but cant figure out how to make it work. The menu system is composed of the letters A-Z and then # for numbers. Each letter is a different button linking to all the reviews starting with that letter. I need to be able to have the user click the letter of choice and then have php read the mysql database and only output tho's movies that start with that first letter or number. I cant find an answer Ive searched and searched and have only found full word matchings. Please Help me, Ive been trying to finish this website and am stuck....

ANSWER:
SELECT DISTINCT SUBSTRING(nameProduct, 1, 1) AS firstLetter FROM products ORDER BY firstLetter

Shores

4:23 pm on Jan 26, 2009 (gmt 0)

10+ Year Member



I you're searching for something that starts with the letter A you should query:

SELECT nameProduct FROM products WHERE nameProduct LIKE "A%"

But i'm not sure if this is what you need...

istojanovic

4:38 pm on Jan 26, 2009 (gmt 0)

10+ Year Member



No, Question is: How to create list of first letters for products in DB. For example, in DB table products I have:
- Product 1
- Product 2
- My Product
- Test Product
- A Product

Now, how to create script which return
- A, M, P, T

The answer is:

SELECT DISTINCT SUBSTRING(nameProduct, 1, 1) AS firstLetter FROM products ORDER BY firstLetter

With:

SELECT nameProduct FROM products WHERE nameProduct LIKE "A%"

Result is:
- A Product
(products start with A)
This is a second step when user click on current letter

Shores

4:56 pm on Jan 26, 2009 (gmt 0)

10+ Year Member



So, you posted both the question and the answer? I'm amazed :)

istojanovic

5:11 pm on Jan 26, 2009 (gmt 0)

10+ Year Member



I want to answer to very old question. Post is closed. So I create new post, with question and answer. :)

coopster

7:06 pm on Jan 26, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Old post being referred to:
[webmasterworld.com...]

Welcome to WebmasterWorld, istojanovic!
Welcome to WebmasterWorld, Shores!