Jump to content

[SOLVED] Looping through multiple results only once each...


ShootingBlanks

Recommended Posts

Hello.

 

I have a result set from a MySQL query that has some duplicates, but I want to list each instance only once...

 

...So, let's say I had a database column called "letters" that had the letters A-J in it.  Then let's say that my result set from my query was:

 

A, A, B, C, F, F, F, F, G, I, I, J

 

(the reason for the duplicates are because of a join query with another table)

 

I would want to "echo" each letter in my result set only once.  So, I'd like to list out:

 

A, B, C, F, G, I, J

 

How would that be possible?  Thanks!!!

 

 

Link to comment
Share on other sites

Use DISTINCT

 

SELECT DISTINCT(letters) FROM ...

 

But I still need all the rest of the result set.  So, for instance (to elaborate on my original example) instead of simply saying that my result set was:

 

A, A, B, C, F, F, F, F, G, I, I, J

 

...let's say that it's actually:

 

COL1  COL2
A    1
A    2
B    3
C    7
F    3
F    4
F    2
F    5
G    1
I    6
I    1
J    3

 

If I used "DISTINCT", then wouldn't I only get one of each letter's entry?  (so, like, I may get the "A, 1" result but not the "A, 2" result, right?)...

 

Using the 2-column result I just wrote above, my ultimate goal is something like this (which should give you more insight as to why I asked my original question):

 

A    1
     2

B    3

C    7

F    3
     4
     2
     5

G    1

I    6
     1

J    3

 

Thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.