Connecting Tech Pros Worldwide Help | Site Map
Reply
 
LinkBack Thread Tools Search this Thread
  #1  
Old August 2nd, 2007, 09:16 AM
Newbie
 
Join Date: Jul 2007
Posts: 7
Default distinct with multiple field

my $cStr="select distinct artist,company from albuminfo ";


i am using this query to select unique artist and company. But the duplicates also get display.

Is any idea.

Thanks
Reply
  #2  
Old August 2nd, 2007, 09:45 AM
dafodil's Avatar
Needs Regular Fix
 
Join Date: Jul 2007
Location: Philippines
Age: 21
Posts: 351
Default

select distinct artist, company from albuminfo
your mysql code up there means that there will be distinct artist and company not distinct artist and distinct company.
For example:
Artist--------Company
----------------------------------------
Artist A------Company B
Artist A---- - Company C
Artist A-------Company D
Artist B-------Company A
Artist C-------Company A

Try this first:
select distinct artist from albuminfo
so that you can check whether distinct really works.
If it works, the problem might be in the way you put the command
Reply
  #3  
Old August 2nd, 2007, 10:55 AM
Newbie
 
Join Date: Jul 2007
Posts: 7
Default

Thanks for your reply,

I have tried to select only the distinct artist,its working.but if i select both artist and company it gives duplicate records.

is any idea.
Reply
  #4  
Old August 2nd, 2007, 11:13 AM
mwasif's Avatar
Moderator
 
Join Date: Jul 2006
Location: Pakistan
Posts: 642
Default

Did you really understand what dafodil told?

What you wanted to display if you have the following data in the table?

Artist--------Company
----------------------------------------
Artist A------Company B
Artist A---- - Company C
Artist A-------Company D
Artist B-------Company A
Artist C-------Company A

Do you want to select Artist A only once regardless of the company? If this is you are looking for then use GROUP BY instead
Expand|Select|Wrap|Line Numbers
  1. SELECT artist,company FROM albuminfo GROUP BY artist
Reply
  #5  
Old August 2nd, 2007, 11:25 AM
dafodil's Avatar
Needs Regular Fix
 
Join Date: Jul 2007
Location: Philippines
Age: 21
Posts: 351
Default

Try this:
Expand|Select|Wrap|Line Numbers
  1. SELECT distinct artist, company FROM albuminfo group by artist

If it doesen't work give me a sample of what would you want to display just like my sample on my previous posts.

Sorry I'm still editting my post when you posted I can't delete this anymore.
try it.

Moderator: Kindly use code tags when posting source code.

Last edited by mwasif; August 2nd, 2007 at 11:30 AM. Reason: Added code tags
Reply
  #6  
Old August 2nd, 2007, 11:40 AM
rajiv07's Avatar
Familiar Sight
 
Join Date: Jun 2007
Location: Chennai
Age: 25
Posts: 141
Default

Thank u,
Let me Explain with Sample,

My table name Is ALBUMINFO

Assume It has two fields called ARTIST and COMPANY

ARTIST --------------------------------------> COMPANY
A1 ------------------------------------------> C1
A1 -------------------------------------------> C1
A2--------------------------------------------> C2
A2 ------------------------------------------> C2
A2 ------------------------------------------> C3
A2 ------------------------------------------> C3
Ok,Now i want output ike this

ARTIST---> A1 and A2
COMPANY-->C1 and C2 and C3
Reply
  #7  
Old August 2nd, 2007, 11:52 AM
mwasif's Avatar
Moderator
 
Join Date: Jul 2006
Location: Pakistan
Posts: 642
Default

How will you know that which Artist belongs to which company?
Reply
  #8  
Old August 2nd, 2007, 11:57 AM
dafodil's Avatar
Needs Regular Fix
 
Join Date: Jul 2007
Location: Philippines
Age: 21
Posts: 351
Default

Yeah that's right it should be by pair.. In the first place, you should have created a table with a primary key as artist and a unique key as company, if you want to totally sort them out.
Reply
  #9  
Old August 2nd, 2007, 12:07 PM
mwasif's Avatar
Moderator
 
Join Date: Jul 2006
Location: Pakistan
Posts: 642
Default

You can also use GROUP_CONCAT().
Reply
  #10  
Old August 2nd, 2007, 12:14 PM
rajiv07's Avatar
Familiar Sight
 
Join Date: Jun 2007
Location: Chennai
Age: 25
Posts: 141
Default

Table ALBUMINFO

ID---ARTIST --------------------------------------> COMPANY
1----A1 ------------------------------------------> C1
2----A1 -------------------------------------------> C1
3----A2--------------------------------------------> C2
4----A2 ------------------------------------------> C2
5----A2 ------------------------------------------> C3
6----A2 ------------------------------------------> C3



here i am using ID as a primary Key.


my $cStr="select distinct artist from albuminfo ";

my $cStr1="select distinct company from albuminfo ";

Suppose, i use these Query in two steps

The OutPut Will be

ARTIST

A1,A2

and

COMPANY

C1,C2,C3

If i am right,Then i want impelment these two Query into one compained query.
for the Same above Output.

Is Any Idea.
Reply
  #11  
Old August 2nd, 2007, 12:31 PM
mwasif's Avatar
Moderator
 
Join Date: Jul 2006
Location: Pakistan
Posts: 642
Default

Try both the queries and see what you require. You can join two queries with UNION.

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT artist FROM albuminfo
  2. UNION ALL
  3. SELECT DISTINCT company FROM albuminfo
OR
Expand|Select|Wrap|Line Numbers
  1. SELECT GROUP_CONCAT(DISTINCT artist) FROM albuminfo
  2. UNION ALL
  3. SELECT GROUP_CONCAT(DISTINCT company) FROM albuminfo
BTW: Did you try GROUP_CONCAT() I mentioned above?
Reply
  #12  
Old August 24th, 2007, 03:17 PM
Newbie
 
Join Date: Jul 2006
Posts: 2
Default

Quote:
Originally Posted by mwasif
Try both the queries and see what you require. You can join two queries with UNION.

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT artist FROM albuminfo
  2. UNION ALL
  3. SELECT DISTINCT company FROM albuminfo
OR
Expand|Select|Wrap|Line Numbers
  1. SELECT GROUP_CONCAT(DISTINCT artist) FROM albuminfo
  2. UNION ALL
  3. SELECT GROUP_CONCAT(DISTINCT company) FROM albuminfo
BTW: Did you try GROUP_CONCAT() I mentioned above?
U can get more info regarding GROUP_CONCAT() Here.
http://www.discussweb.com/database-s...cat-mysql.html
Reply
Reply

Bookmarks

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 On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,248 network members.