Log in

View Full Version : Really difficult- can you help?



MrRSMan
12-27-2009, 12:25 AM
I have a field in my database with values that INCREASE (they never decrease). I need to find a way of selecting the NON-DUPLICATE rows.


NUM

1
2
2
3
4
4
4
4
5
5
6
7
8
8

IMPORTANT

The DISTINCT keyword is no use to me. By using the distinct keyword for the example above I'd be left with only the values that occur once. But I also want ONE OF EACH of the duplicated rows to be left as well.

So the example above would become:


NUM

1
2
3
4
5
6
7
8

This is because the values that occur only once are selected (this is what DISTINCT does) but then the values that occur multiple times are selected only ONCE.

I'm sorry if I sound like I've over-explained this, but I've had trouble trying to tell people what I need- people keep recommending the DISTINCT keyword.

Thanks in advance- I hope you can help.

djr33
12-27-2009, 02:42 AM
I don't know of a way to do this in mysql. The basic method I can think of is using the number as some sort of index, then if you add it again it will replace it. This is easily done in PHP, using a number of methods. For example, you could use them in an array:
loop through each result with:
$array[$number] = [whatever];
Then you could store it that way. Or if you just want the numbers, you could store them as the values of the array and just not copy it if it already exists. Or use one of the sort functions that only returns unique values.

As for mysql, the only idea that I have is to simulate creating a new table (there's some method I've heard of, but can't remember how), then copy values like in the php array-- overwriting those that are not unique.