doujiao3072 2015-01-23 13:35
浏览 13
已采纳

使用单个查询从表A中的值计算表B上的结果数

Is it possible to count the number of results matched in table B from values drawn from table A in the same query?

For example -

tableA
--------------
color  | type
-------|------
blue   | car
red    | truck
orange | car
green  | car

-

tableB
--------------
color  | sku
-------|------
blue   | 1
red    | 2
orange | 3
green  | 4
orange | 5
green  | 6

Question - How many rows in tableB are related to type = car? Result should return 5.

Using two queries and some PHP this is pretty straightforward, I was just curious if this is possible with a single MySQL query, and if it would be more efficient.

  • 写回答

3条回答 默认 最新

  • dongzai5181 2015-01-23 13:46
    关注

    Try the below query

    SELECT count(*) AS count 
    FROM tableB b, tableA a 
    WHERE b.color = a.color 
    AND type = 'car';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看