嗨,这是我的数据结构:
$(document).ready(function() {
$('#noscript-description').css({
'display' : 'none'
// Just change the none to block to see the results
});
});
我想输出: 所有类别和每个类别都应显示:
期望的输出:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<body>
<p id="noscript-description">This can be seen if you <b>DO NOT</b> have JavaScript enabled.</p>
<!-- I have added in jQuery, if you never realised -->
</body>
</html>
到目前为止我的查询:
categories
id name
----------------
1 dogs
2 cats
3 birds
images
id name cat datetime views
---------------------------------------
1 dog1 1 2015-10-01 123
2 dog2 1 2015-10-02 45
3 cat1 2 2015-10-03 678
4 cat2 2 2015-11-01 901
5 cat3 2 2015-11-02 234
6 bird1 3 2015-12-03 456
问题是:我如何获得最后一个字段cat name images views latest id_latest
----------------------------------------------------
1 dogs 2 123 2015-10-02 5
2 cats 3 901 2015-11-02 4
3 birds 1 456 2015-12-03 6
?
感谢
答案 0 :(得分:0)
Select t.*, i.id as imageID
from
(Select
categories.*,
Count(images.id) As images,
Sum(images.views) As views,
Max(images.datetime) As latest
From categories
Left Join
images
On images.catid = categories.id
Group By categories.id
) t
left join
images i
on i.`datetime`= latest
答案 1 :(得分:-1)
categories
catid name
----------------
1 dogs
2 cats
3 birds
images
imgid name catid datetime views
---------------------------------------
1 dog1 1 2015-10-01 123
2 dog2 1 2015-10-02 45
3 cat1 2 2015-10-03 678
4 cat2 2 2015-11-01 901
5 cat3 2 2015-11-02 234
6 bird1 3 2015-12-03 456
代码:
Select
categories.catid,
categories.name,
Count(images.imgid) As images,
Sum(images.views) As views,
Max(images.datetime) As latest
From
categories
Left Join images On images.catid = categories.catid
Group By
categories.catid, categories.name