Connecting Tech Pros Worldwide Help | Site Map

distinct with multiple field

  #1  
Old August 2nd, 2007, 09:16 AM
Newbie
 
Join Date: Jul 2007
Posts: 7
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
  #2  
Old August 2nd, 2007, 09:45 AM
dafodil's Avatar
Needs Regular Fix
 
Join Date: Jul 2007
Location: Philippines
Posts: 393

re: distinct with multiple field


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
  #3  
Old August 2nd, 2007, 10:55 AM
Newbie
 
Join Date: Jul 2007
Posts: 7

re: distinct with multiple field


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.
  #4  
Old August 2nd, 2007, 11:13 AM
mwasif's Avatar
Moderator
 
Join Date: Jul 2006
Location: Pakistan
Posts: 711
Provided Answers: 1

re: distinct with multiple field


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
  #5  
Old August 2nd, 2007, 11:25 AM
dafodil's Avatar
Needs Regular Fix
 
Join Date: Jul 2007
Location: Philippines
Posts: 393

re: distinct with multiple field


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
  #6  
Old August 2nd, 2007, 11:40 AM
rajiv07's Avatar
Familiar Sight
 
Join Date: Jun 2007
Location: Chennai
Posts: 141

re: distinct with multiple field


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
  #7  
Old August 2nd, 2007, 11:52 AM
mwasif's Avatar
Moderator
 
Join Date: Jul 2006
Location: Pakistan
Posts: 711
Provided Answers: 1

re: distinct with multiple field


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

re: distinct with multiple field


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.
  #9  
Old August 2nd, 2007, 12:07 PM
mwasif's Avatar
Moderator
 
Join Date: Jul 2006
Location: Pakistan
Posts: 711
Provided Answers: 1

re: distinct with multiple field


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

re: distinct with multiple field


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.
  #11  
Old August 2nd, 2007, 12:31 PM
mwasif's Avatar
Moderator
 
Join Date: Jul 2006
Location: Pakistan
Posts: 711
Provided Answers: 1

re: distinct with multiple field


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?
  #12  
Old August 24th, 2007, 03:17 PM
Newbie
 
Join Date: Jul 2006
Posts: 2

re: distinct with multiple field


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to query database with multiple queries Cady Steldyn answers 1 November 13th, 2005 03:54 AM
select statement with multiple value where rhamlin answers 3 November 8th, 2005 09:05 PM
having mySQL find only orders with multiple rows in another table news@celticbear.com answers 2 July 23rd, 2005 08:08 AM
How to query database with multiple queries Cady Steldyn answers 1 July 20th, 2005 05:48 AM