Forum Moderators: coopster

Message Too Old, No Replies

PHP, MySQL question

Limit a query based on record

         

russkern

7:54 pm on Jun 8, 2010 (gmt 0)

10+ Year Member



Hoping someone can help with a MySQL Query...

I have created a select list off of a DB table...

What I am creating is an Automotive drop down list. The user selects the Vehicle year and I query the database to show me all makes for that year.

The list I was given was ordered YEAR,MAKE,MODEL,ENGINE and I imported it into my DB table that way

When I ask it to return every MAKE where YEAR == X, I may get 5 Honda (because the database contains 1 line for each model), 10 Chevrolet, etc. I want to only print to the list 1 Honda and 1 Chevrolet.

How do I limit this?

I hope this makes sense...

R

Demaestro

7:57 pm on Jun 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Can you post the sql query that you are using that is fetching the results?

russkern

8:03 pm on Jun 8, 2010 (gmt 0)

10+ Year Member



Sure can

$query = "SELECT `make` FROM `vehicle` WHERE `year`=$year";

each "Make" may appear between 1 an 10 times... I only want to print 1 of each to the list.

Demaestro

8:20 pm on Jun 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Try

SELECT distinct `make` FROM `vehicle` WHERE `year`=$year"

Or

SELECT `make` FROM `vehicle` WHERE `year`=$year group by make"

Alcoholico

8:25 pm on Jun 8, 2010 (gmt 0)

10+ Year Member



I think this could work:

$query = "SELECT make FROM vehicle WHERE year=$year GROUP BY make";

russkern

8:38 pm on Jun 8, 2010 (gmt 0)

10+ Year Member



What an awesomely helpful bunch of people.

Thank you for the quick reply.

R

Demaestro

8:50 pm on Jun 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



:)

A pleasure to help. I have been helped many times, just paying it back/forward.

If you need more help post back

russkern

9:10 pm on Jun 8, 2010 (gmt 0)

10+ Year Member



Will do... funny thing is that when I was trying to figure this out myself (always spend at least an hour before asking) I ran across this but didn't think it was what I needed. Thanks again.