Jump to content

Recommended Posts

Hey guys im having a issue with getting DISTINCT to work

 

im pulling 2 columns using pagination

 

so im counting first and then selecting but it still giving me duplicate owners

 

SELECT COUNT(DISTINCT owner) FROM planets WHERE id != ''

SELECT DISTINCT owner, homes FROM planets WHERE id != '' ORDER BY homes DESC LIMIT $offset, $rowsperpage

Link to comment
https://forums.phpfreaks.com/topic/268406-problems-with-using-distinct/
Share on other sites

Are you certain that the "owner" values are indeed identical? You could also try with GROUP BY, but should give pretty much the same result as DISTINCT.

Use var_dump () to check the data you're retrieving, and post some example data showcasing the problem here.

I think i got the correct return now using group by

 

SELECT COUNT(*) FROM planets WHERE id != '' group by owner

SELECT owner,$building FROM planets WHERE id != '' group by owner ORDER BY $building DESC LIMIT $offset, $rowsperpage

Its a list of planets

 

players own more then one planet

 

each planet has a level of homes on them

 

im trying to grab the players with the highest level of homes in order with out having duplicates.

 

 

so like lets say one player is holding 1st and 2nd place of the highest level of homes I dont want it to grab the 2nd place only the 1st.

 

lol hope that makes sense.

:P nah no total

 

 

In the picture i posted

 

Notice LordDougy is holding 1st ,5th and 10th place

 

i dont want his 5th and 10th place to be included

 

 

so Matei should be in 5th instead.

 

so once it grabs a user it no longer grabs that user again

Hmm... I should perhaps have clarified my previous post, and made it clear it was my previous statement (question, really) that I was referring to. Not the desired end results.

 

Jesi: I don't think you need DISTINCT in there.

table2.png

 

that gives me this return

 

I think its correct but its doing it backwards grabing every users lowest amount

 

 

ChristainF: ya i understood you wanted me to write statement but i have no idea :( i couldnt even write it in english so you guys could understand what im trying to do :P hehe

If all you want is the maximum value for each owner, you would use MAX(homes) as mh, then GROUP BY owner. You would also ORDER BY mh DESC to get the result in the order that you want them.

 

However, if you want the specific row holding the maximum for each owner (perhaps you want the planet name or the row id), you would need to do this - http://dev.mysql.com/doc/refman/5.5/en/example-maximum-column-group-row.html

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.