我有两张表来获得展示,然后点击
Product
+----+--------+
| PID| brand |
+----+--------+
| 1 | Ford |
| 2 | Toyota |
| 6 | Holden |
+----+--------+
States
+----+--------+------+--------+------------+
| ID | PID |CLICKS| VIEWS | DATE |
+----+--------+------+--------+------------+
| 1 | 1 | 1 | 0 | 12/12/2015|
| 1 | 1 | 1 | 0 | 12/12/2015|
| 2 | 2 | 1 | 0 | 12/12/2015|
| 3 | 2 | 0 | 1 | 12/12/2015|
| 3 | 1 | 0 | 1 | 12/12/2015|
+----+--------+------+--------+------------+
我需要得到一些像这样的结果
+--------+------+--------+
| PID |CLICKS| VIEWS |
+--------+------+--------+
| 1 | 2 | 1 |
| 2 | 1 | 0 |
+--------+------+--------+
有可能吗?我已经尝试过多次未来的故障数据
答案 0 :(得分:1)
SELECT
Products.PID,
SUM(CLICKS) as CLICKS,
SUM(VIEWS) as VIEWS
FROM Products
LEFT JOIN States ON States.PID=Products.PID
GROUP BY Products.PID
如果所有产品需要0
- (CGFloat)carouselItemWidth:(iCarousel *)carousel
{
//usually this should be slightly wider than the item views
return 240;
}
答案 1 :(得分:1)
通过Inner Join使用它。
SELECT t1.pid, sum(clicks) as CLICKS, sum(views) as VIEWS
FROM Product as t1 INNER JOIN States as t2 ON t1.pid=t2.pid GROUP BY t1.pid