当前位置: 动力学知识库 > 问答 > 编程问答 >

Mysql results differs at evens from singles in same query

问题描述:

I have a strange error first time seing such results maybe someone else also found how to overpass such odd error.

I have a table structure like this :

|id|uid|test1|test2|rid|

1 1 - - 1

2 1 - - 1

3 2 - - 2

4 2 - - 2

5 3 - - 3

6 3 - - 3

7 4 - - 4

8 4 - - 4

----------------------------

as "-" are varchar data all other columns are integers

My query with the odd results is this :

SELECT COUNT(uid) AS COUNT,

id AS ID,

uid AS InqID,

test1 AS A,

test2 AS B

FROM test_table

WHERE rid = (X)

GROUP BY uid

ORDER BY id DESC


As results i get

if (X) = 1 or 3 it shows me the correct last id number (2 id for 1 and 6 id for 3)

but if (X) is 2 or 4 it shows me the first in row id number (3 id for 2 instead of 4 and 7 id for 4 instead of 8)

Can anyone tell me why i get the correct results only as singles in rid and not at even numbers of rid column or how at least this query will work as it has to ?

Thank you all in advance

网友答案:

Use MAX(id) instead of ORDER BY.

SELECT COUNT(uid) AS COUNT, MAX(id) AS ID, uid AS InqID, test1 AS A, test2 AS B 
FROM test_table 
WHERE rid = (X) 
GROUP BY uid 
分享给朋友:
您可能感兴趣的文章:
随机阅读: