我的查询正在检查5个评论网站的数据并返回site_id,review_count& review_average。
如果评论网站没有数据,那么我想返回0作为计数&平均。
这可以在mysql查询中执行吗?
MySQL的:
SELECT rrss.review_site_id,rrss.review_count,rrss.review_average,rs.name
FROM rooftops_review_sites_snapshots rrss
LEFT JOIN review_sites rs ON rrss.review_site_id = rs.id
WHERE rrss.rooftop_id = 185
AND rrss.import_id = 16
AND rrss.review_site_id IN (31,30,12,10,29)
当前输出:
Array
(
[google] => Array
(
[review_site_id] => 31
[review_count] => 24
[review_average] => 3.80
)
[edmunds] => Array
(
[review_site_id] => 12
[review_count] => 8
[review_average] => 4.50
)
)
期望的输出:
Array
(
[google] => Array
(
[review_site_id] => 31
[review_count] => 24
[review_average] => 3.80
)
[edmunds] => Array
(
[review_site_id] => 12
[review_count] => 8
[review_average] => 4.50
)
[yelp] => Array
(
[review_site_id] => 31
[review_count] => 0
[review_average] => 0
)
[dealerrater] => Array
(
[review_site_id] => 12
[review_count] => 0
[review_average] => 0
)
[cars] => Array
(
[review_site_id] => 12
[review_count] => 0
[review_average] => 0
)
)
答案 0 :(得分:0)
更改表的位置并使用ISNULL()。查看下面的查询。
SELECT rs.id,rs.name, ISNULL(rrss.review_count, 0), ISNULL(rrss.review_average, 0) FROM review_sites rs LEFT JOIN rooftops_review_sites_snapshots rrss ON rrss.review_site_id = rs.id WHERE rrss.rooftop_id = 185 AND rrss.import_id = 16 AND rrss.review_site_id IN (31,30,12,10,29)
答案 1 :(得分:0)
尝试这个
SELECT
rrss.review_site_id,
ISNULL(rrss.review_count, 0),
ISNULL(rrss.review_average, 0),
rs.name
FROM rooftops_review_sites_snapshots rrss
LEFT OUTER JOIN review_sites rs
ON rrss.review_site_id = rs.id
WHERE rrss.rooftop_id = 185
AND rrss.import_id = 16
AND rrss.review_site_id IN (31,30,12,10,29)