If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > SQL Select Problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-04-07, 19:51
lslars31 lslars31 is offline
Registered User
 
Join Date: Jun 2003
Location: Minnesota
Posts: 61
SQL Select Problem

I have a db full of articles that have a category field with the name of the category the articles are from.

Is there anyway to select and display just the different types of categories on the db?

What I am trying to do is make a list of the categories in the database without already knowing which categories there are so I can make a menu with links.

Somebody said Joins do this but I am not familiar with them.
__________________
Mr. Lars
Reply With Quote
  #2 (permalink)  
Old 03-04-07, 19:54
lslars31 lslars31 is offline
Registered User
 
Join Date: Jun 2003
Location: Minnesota
Posts: 61
Just found out that joining isn't the answer. I only have one table...
__________________
Mr. Lars
Reply With Quote
  #3 (permalink)  
Old 03-04-07, 20:48
dbmab dbmab is offline
Registered User
 
Join Date: Apr 2006
Location: Denver, Co. USA
Posts: 240
You can actually join a table to itself. However, if I understand what you want, you don't need to use a join.

If you have a table with a category column, and you want to get a list of distinct categories, the following query will work (note: You can only select the column that you want a distinct list of. If you add a second column to the SELECT list, it will be considered when looking for distinct results.)

SELECT DISTINCT category FROM your_table GROUP BY category
Reply With Quote
  #4 (permalink)  
Old 03-04-07, 21:26
lslars31 lslars31 is offline
Registered User
 
Join Date: Jun 2003
Location: Minnesota
Posts: 61
BEAUTIFUL. Thanks so much.
__________________
Mr. Lars
Reply With Quote
  #5 (permalink)  
Old 03-04-07, 21:35
lslars31 lslars31 is offline
Registered User
 
Join Date: Jun 2003
Location: Minnesota
Posts: 61
what does "GROUP BY" do?

If it is selecting distinctly then why do you need to group them when there would be nothing to group it with?
__________________
Mr. Lars
Reply With Quote
  #6 (permalink)  
Old 03-05-07, 00:49
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
exactly

this is too much -- SELECT DISTINCT category FROM your_table GROUP BY category

it should be either this -- SELECT DISTINCT category FROM your_table

or this -- SELECT category FROM your_table GROUP BY category
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On