Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Coding Forum


You are currently viewing our Coding Forum as a guest. Please register to participate.
Login



Reply
Old 01-10-2010, 11:39 AM SQL Distinct order
Extreme Talker

Posts: 246
Trades: 0
I am using the following to grab a distinct list of descriptions from a table:

Select Distinct Description From TableName

The Descriptions are in a certain order in the database and I'd like to show them in that same order. Problem is that when you run the above query the first distinct description is actually the 37th. How do I maintain the order?
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
stbuchok is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-10-2010, 11:44 AM Re: SQL Distinct order
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,371
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
ORDER BY what_ever_column_defines_the_order_you_want
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-10-2010, 11:54 AM Re: SQL Distinct order
Extreme Talker

Posts: 246
Trades: 0
Yes but that orders it alphabetically, I want it ordered by the way it is in the table. (I have a way to get this to work but I'd have to modify the table, which I'd prefer not to do)
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
stbuchok is offline
Reply With Quote
View Public Profile
 
Old 01-10-2010, 11:55 AM Re: SQL Distinct order
Extreme Talker

Posts: 246
Trades: 0
sorry I responded without completely reading your answer, yes you are right but I think I'll have to add a column.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
stbuchok is offline
Reply With Quote
View Public Profile
 
Old 08-16-2010, 03:25 AM Re: SQL Distinct order
Junior Talker

Posts: 1
Name: ajoe
Trades: 0
mysql> select * from products;

+---------+-------------+-----------+------+
| prod_id | prod_source | prod_type | flag |
+---------+-------------+-----------+------+
| 1 | USA | 2 | 0 |
| 2 | USA | 2 | 0 |
| 3 | USA | 2 | 0 |
| 4 | USA | 3 | 1 |
| 5 | USA | 3 | 0 |
| 6 | BRA | 1 | 1 |
| 7 | BRA | 2 | 1 |
+---------+-------------+-----------+------+


Required resulset
==================
+-----------+----------+
| prod_type | count(*) |
+-----------+----------+
| 2 | 3 |
| 3 | 0 |
| 4 | 0 |
+-----------+----------+


where condition
prod_source = USA
flag=0


but the resultset i'm getting is

+-----------+----------+
| prod_type | count(*) |
+-----------+----------+
| 2 | 3 |
+-----------+----------+


with the query SELECT prod_type ,count(*)
FROM products
WHERE prod_source='USA' and flag=0
GROUP by prod_type
ORDER by prod_type;


HOW do i get the resulset with count of all the prod_type's in USA ? It must display all the prod_type's in USA even if the count is 0 .






TABLE DEFINITION
===================

CREATE TABLE `products` (
`prod_id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`prod_source` VARCHAR(45) NOT NULL,
`prod_type` INTEGER UNSIGNED NOT NULL,
`flag` INTEGER UNSIGNED NOT NULL,
PRIMARY KEY (`prod_id`)
)
ENGINE = InnoDB;


DATA INSERTION
===============

INSERT INTO `products` VALUES (1,'USA',2,0),(2,'USA',2,0),(3,'USA',2,0),(4,'USA' ,3,1),(5,'USA',4,1),(6,'BRA',1,1),(7,'BRA',2,1);

http://forums.mysql.com/read.php?134...959#msg-380959
ajoe joseph is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to SQL Distinct order
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.69689 seconds with 12 queries