首先为newbee问题道歉,我对MySQL很新。
我有3张桌子用于简单的摄影比赛:
注册:
ID
UserName
name
LastName
Email
Mobile
照相馆:
ID
ImageName
user_id
Status=1
photovote:
Photo_ID
Date
Time
Status
我需要在给定时间片上计算每张照片的投票数,如果出现平局,我需要先检查收到最后一张投票的那张照片。
我提出的查询是:
SELECT v.Photo_ID AS "Photo ID",
g.ImageName AS "Photo URL",
r.UserName AS "Username",
r.name AS "Name",
r.LastName AS "Surname",
r.Email AS "E-Mail",
r.Mobile AS "Phone",
v.Date AS "Last Vote Date",
v.Time AS "Last Vote Time",
COUNT(Photo_ID) AS "Total Votes"
FROM photovote v
INNER JOIN photogallery g ON v.Photo_ID = g.ID
INNER JOIN registration r ON g.user_id = r.ID
WHERE v.Status =1
AND g.Status =1
AND v.Date >20130728
AND v.Date <20130805
GROUP BY 1
ORDER BY 10 DESC, 8 DESC, 9 DESC
然而,ORDER BY无法正常工作。任何暗示都是非常令人沮丧的。
谢谢, 安德烈
答案 0 :(得分:1)
将此ORDER BY替换为:
ORDER BY `Last Vote Time` DESC, `Last Vote Date` DESC, `Total Votes` DESC;
由于“”(空格)字符,您需要使用反向标记。 ORDER BYs的排序也有所不同。
此外,您需要在GROUP BY中包含更多字段,否则数据将在MYSQL中丢失(其他DBMS会使您的查询无效)。
将这些添加到您的GROUP BY:
GROUP BY v.Photo_id, `Last Vote Date`,`Last Vote Time`
(如果其他任何事情随着时间的推移而发生变化(例如名称),那么这可能会在以后出现问题 - 但这是一个单独的问题)
答案 1 :(得分:0)
我需要在给定时间片上计算每张照片的投票数,如果出现平局,我需要先检查收到最后一张投票的那张照片。
只是为了获得按投票和lastVote时间排序的photo_ids:
select Photo_ID, count(*) as votes, max(Timestamp(Date,Time)) as lastVoteTime
from photovote
where Date > [start] and Date < [end] and status = 1
group by Photo_ID
order by votes desc, lastVoteTime asc;
您可以将此查询的结果与其他表联接以获得所需的结果。