2

SQL yapısı

There is a problem with my MySql query. Could you help me out with it?

I have a database as in the picture above, and there are userids that belongs to each user. It should be sorted by type descending. It records the time with the PHP time() function which is written at that moment in the datetime cell within the database. However, the SQL query which I used gives me a result as in the picture below:

SELECT 
  user.username, 
  kim.userid,
  kim.subject, 
  count(kim.userid), 
  max(kim.dateline),
  max(kim.rateid) 
FROM test as kim 
INNER JOIN user 
WHERE user.userid = kim.userid 
GROUP BY kim.userid limit 10)

enter image description here

Although I get the right results, I’m still having a little problem since the rateid does not show the right subject. So, I’ve been searching for it for two days, and I think there’s a problem that I don't understand or cannot see.

The right query should be like the on in the following picture: enter image description here

I appreciate if you help!

4

3 回答 3

1

您正在尝试从具有最大 rateid 的行中获取信息以及汇总信息。为此,您需要重新加入原始表:

select kim.userid, u.username, kim.cnt, kim.maxdate, kim.maxrate, t.subject
from (SELECT kim.userid, count(kim.userid) as cnt,
             max(kim.dateline) as maxdate, max(kim.rateid) as maxrate
     FROM test kim 
     GROUP BY kim.userid
     limit 10
    ) kim INNER JOIN
    user u
    on u.userid = kim.userid join
    test t
    on kim.userid = t.userid and
       kim.maxrate = t.rateid

这会找到每个用户的最大速率,然后返回到表中以获取该速率的主题。您的maxdate专栏似乎是您想要的。如果您想要日期,maxrate那么您可能还想从中获取t

于 2013-05-11T16:25:46.313 回答
0

尝试这个

Select user.username, kim.userid ,kim.subject, count(kim.userid), max(kim.dateline),
max(kim.rateid) 
from test as kim
left join user ON user.userid = kim.userid group by kim.userid limit 10
于 2013-05-11T14:01:18.387 回答
0

谢谢关注,

我找到了我的问题的解决方案。解决方案应该是这样的:

从测试中选择主题
userid=user.userid ORDER BY dateline desc LIMIT 1)
 作为主题,
 (从测试中选择计数(*)
 WHERE userid=user.userid) 作为 adet
 FROM 用户限制 10
于 2013-05-13T21:56:56.580 回答