SQL help - filtering data...

They have: 5,633 posts

Joined: Jan 1970

This will be an easy one for someone:

Let's say for example, I have a table called 'Directions' with a field in it called 'Directions'. In that field there are many entries, with some repeating themselves like this:

North
NorthEast
South
North
South
SouthEast
NorhEast
South
North

I want to query this table but just pull the data with no repeats. When I query the table above, I just want to get this info back in my results:

North
NorthEast
South
SouthEast

I'm planning on using this to fill in a combo box. And I dont want repeats in my combo box.

Thanks in advance

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I'm not sure, you read about the mySQL SELECT query here.

I *think* that this will do it:
SELECT DISTINCT Directions FROM Directions ... blah blah blah

Mark Hensler
If there is no answer on Google, then there is no question.

Peter J. Boettcher's picture

They have: 812 posts

Joined: Feb 2000

That is correct SQL syntax. If you can avoid it you should try to store those values (north,south,...) in a separate table. Using DISTINCT will almost always slow down your queries.

PJ | Are we there yet?
pjboettcher.com

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

have you though of using a relational DB layout?

Compass
+--------------+-----------+
| Direction_id | Direction |
+--------------+-----------+
|            1 | North     |
|            2 | South     |
|            3 | East      |
|            4 | West      |
|            5 | NorthWest |
+--------------+-----------+

Directions
+----+--------------+---
| ID | Direction_id |  
+----+--------------+---
|  1 |            2 |  
|  2 |            4 |  
|  3 |            3 | 
|  4 |            5 |  
|  5 |            1 |   
+----+--------------+---
'
Then you can use the 'Compass' table to populate your dropdown menus.

Mark Hensler
If there is no answer on Google, then there is no question.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.