mysql查询某个数到某个数,需要帮助找出MySQL查询计数如果某个数字

I have a table setup like this:

fruit1 | fruit2 | fruit3 | fruit4 | number1 | number2 | number3 | number4

--------------------------------------------------------------------------

apple | orange | banana | berry | 5 | 2 | 1 | 4

orange | banana | apple | berry | 3 | 2 | 5 | 2

berry | banana | orange | apple | 1 | 2 | 5 | 2

I need a MySQL query to count how many times a given fruit is number 5. In this example, apple is 5 twice and orange is 5 once. Does that make sense? fruit1's number is number1, fruit2's number is number2, etc...

I could do this with a bit of php code but I know it's a pretty simple MySQL query. I just have the hardest time putting queries together.

Thanks in advance!

解决方案SELECT fruit, COUNT(*) NumberOfInstance

FROM

(

SELECT fruit1 fruit, number1 num FROM table1

UNION ALL

SELECT fruit2 fruit, number2 num FROM table1

UNION ALL

SELECT fruit3 fruit, number3 num FROM table1

UNION ALL

SELECT fruit4 fruit, number4 num FROM table1

) s

WHERE num = 5

GROUP BY fruit

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值