我有3个表:broadcasts,broadcasts_likes和broadcasts_viewers:
我想要返回结果包括total_likes,total_viewers但是当我运行这个sql时,期望是错误的,你能帮我检查一下吗?我只是想正常使用sql,不要使用子查询。这是我的sql:
<manifest ...>
<!-- Include required permissions for Google Mobile Ads to run-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application>
//...
<!--Include the AdActivity configChanges and theme. -->
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
</application>
</manifest>
答案 0 :(得分:1)
最简单的解决方案是切换到mov ecx, msg
:
count(distinct)
如果广播有很多观点和很多喜欢的话,那么这个节目不会特别好。在这种情况下,您可以在加入或使用相关子查询之前聚合表:
SELECT `b`.*,
COUNT(DISTINCT `bl`.profile_id`) AS `total_likes`,
COUNT(DISTINCT `bv`.view_profile_id) AS `total_viewers`
FROM `broadcasts` `b` LEFT JOIN
`broadcasts_likes` `bl`
ON `b`.`broadcast_id` = `bl`.`broadcast_id` LEFT JOIN
`broadcasts_viewers` `bv`
ON `b`.`broadcast_id` = `bv`.`broadcast_id`
GROUP BY `b`.`broadcast_id`;
实际上,对于SELECT `b`.*,
(SELECT COUNT(*)
FROM broadcasts_likes bl
WHERE `b`.`broadcast_id` = `bl`.`broadcast_id`
) as `total_likes`,
(SELECT COUNT(*)
FROM broadcasts_viewers bv
WHERE `b`.`broadcast_id` = `bv`.`broadcast_id`
) as `total_viewers`
FROM `broadcasts` `b`;
和broadcasts_likes(broadcast_id)
上的索引,这应该具有非常好的性能。