MySQL - 返回匹配查询数据的行数? [英] MySQL - Return number of rows matching query data?

查看:456
本文介绍了MySQL - 返回匹配查询数据的行数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个查询如下:

I have a query as follows:

SELECT 1 FROM shop_inventory a JOIN shop_items b ON b.id=a.iid AND b.szbid=3362169 AND b.cid=a.cid WHERE a.cid=1 GROUP BY a.bought


$ b $我只需要处理这些数据就可以计算出返回的行数(我可以用 mysqli - > num_rows; 我想知道是否有方法返回匹配查询的行数,而不必运行 num_rows

The only thing I need to do with this data is work out the number of rows returned (which I could do with mysqli -> num_rows;. However, I would like to know if there is a method to return the number of rows that match the query, without having to run num_rows?

例如,查询应返回一行,结果为 number_of_rows

For example, the query should return one row, with one result, number_of_rows.

I希望这是有道理的!

I hope this makes sense!

推荐答案

select count(*) as `number_of_rows`
from (
    select 1
    from shop_inventory a
    join shop_items b on b.id = a.iid
        and b.szbid = 3362169
        and b.cid = a.cid
    where a.cid = 1
    group by a.bought
) a

在这种情况下,由于您没有使用任何聚合函数,并且 GROUP BY 仅仅是为了消除重复,你也可以这样做:

In this case, since you are not using any aggregate functions and the GROUP BY is merely to eliminate duplicates, you could also do:

select count(distinct a.bought) as `number_of_rows`
from shop_inventory a
join shop_items b on b.id = a.iid
    and b.szbid = 3362169
    and b.cid = a.cid

这篇关于MySQL - 返回匹配查询数据的行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆