ds08541 2018-08-07 14:56
浏览 374
已采纳

如果大于0,则计算行数和返回结果

I have try to display result only if content > 0

SELECT actors.*, (SELECT count(*) FROM `content_actors`
                  WHERE content_actors.actor = actors.record_id) AS getCount
FROM actors

If i try to add in query

WHERE getCount > 0

I will have error

Unknown column 'getCount' in 'where clause'

  • 写回答

1条回答 默认 最新

  • dongxiong5546 2018-08-07 14:57
    关注

    In MySQL, you can use a having clause:

    SELECT actors.*,
           (SELECT count(*) FROM `content_actors` WHERE content_actors.actor = actors.record_id) AS getCount
    FROM actors
    HAVING getCount > 0;
    

    This is a MySQL extension.

    Assuming actors.record_id is unique (presumably a primary key), this could be written as:

    SELECT a.*, COUNT(*) as getCount
    FROM actors a JOIN
         content_actors ca
         ON ca.actor = a.record_id
    GROUP BY a.record_id;  -- assumes this is unique/primary key
    

    No filtering is needed, because the JOIN requires at least one match.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器