2012-08-29 32 views
0

我有一个查询,如下所示:MySQL - 返回匹配查询数据的行数?

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 

我需要这方面的数据做的唯一一件事就是制定出返回的行数(我可以用mysqli -> num_rows;做不过,我想知道,如果有返回与查询匹配的行数,而不必运行num_rows的方法?

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

我希望这是有道理的!

+0

将从外部查询返回做的计数? –

+0

是的,这很好,除非有更快的方法来做到这一点。 –

+0

嘿,看起来我已经被打败了答案 –

回答

4
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仅仅是消除重复,你也可以这样做:

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 
+0

谢谢:)。 “SELECT COUNT(1)”会更快吗? –

+0

@KeirSimmons查看我的更新。 – RedFilter