我正在执行以下脚本:
SELECT a.*,
gg.image as 'recent_image_file'
FROM
( SELECT c.cid,
c.category_name AS 'cat_name',
COUNT(g.id) AS image_count,
c.category_image AS 'images',
c.estgendpref,
MAX(n.id) as 'recent_image'
FROM tbl_category c
LEFT JOIN tbl_gallery_category g
ON c.cid=g.cat_id
LEFT JOIN tbl_gallery n
ON n.id=g.image_id
WHERE n.date_active < NOW()
GROUP BY c.cid
ORDER BY c.category_name ASC
) a
INNER JOIN tbl_gallery gg
ON a.recent_image = gg.id;
除了MAX(n.id)忽略WHERE子句并返回类别的最高id之外,一切都返回正常。如何让MAX(n.id)返回结果集的最大值,包括WHERE子句?感谢。
答案 0 :(得分:0)
你应该在on子句中使用where条件添加一个正确的AND ..否则,where where work as inner join condition
SELECT a.*,
gg.image as 'recent_image_file'
FROM
( SELECT c.cid,
c.category_name AS 'cat_name',
COUNT(g.id) AS image_count,
c.category_image AS 'images',
c.estgendpref,
MAX(n.id) as 'recent_image'
FROM tbl_category c
LEFT JOIN tbl_gallery_category g ON c.cid=g.cat_id
LEFT JOIN tbl_gallery n ON n.id=g.image_id and n.date_active < NOW()
GROUP BY c.cid
ORDER BY c.category_name ASC
) a
INNER JOIN tbl_gallery gg
ON a.recent_image = gg.id;
答案 1 :(得分:0)
因此,感谢那些建议的人,我发现正在使用WHERE子句。然而,MAX(id)并不是真正想要的,因为较低的id可能有更大的日期。以下作品很有魅力。
def __init__(self):
# http://stackoverflow.com/a/4140988/675216
vcmd= (self.root.register(self.validate_spin), '%W', '%P')
# Rest of code left out
self.spin.config(validate="key", validatecommand=vcmd)
self.spin.bind("<<ResetValidate>>", self.on_reset_validate)
def on_reset_validate(self, event):
# Turn validate back on and set textVariable
self.spin.config(validate="key")
def validate_spin(self, name, nv):
# Do validation ...
if not valid:
self.spin.event_generate("<<ResetValidate>>", when="tail")
return valid