2012-08-29 31 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