1

サイトの別の投稿から、次のタイプのクエリを取得しました。彼らは合計を使用していました。リストの数を探しています。

SELECT
COUNT(IF(sold_price < 20,1,0)) as 'Under 20',
COUNT(IF(sold_price BETWEEN 20 and 50,1,0)) as '20 - 50',
COUNT(IF(sold_price BETWEEN 50 and 100,1,0)) as '50 - 100',
COUNT(IF(sold_price BETWEEN 100 and 250,1,0)) as '100 - 250',
COUNT(IF(sold_price BETWEEN 250 and 500,1,0)) as '250 - 500',
COUNT(IF(sold_price BETWEEN 500 and 1000,1,0)) as '500 - 1000',
COUNT(IF(sold_price BETWEEN 1000 and 2000,1,0)) as '1000 - 2000',
COUNT(IF(sold_price > 2000,1,0)) as 'Over 2000'
FROM listings
where current_batch = 'Y'

すべての結果が同じ数値として返されます

Under 20    20 - 50 50 - 100    100 - 250   250 - 500   500 - 1000  1000 - 2000 Over 2000
94665   94665   94665   94665   94665   94665   94665   94665

これを行うためのショーに関する提案はありますか、またはこれがカウントで実行できるかどうかはありますか?

4

2 に答える 2

3

MySQL のドキュメントには次のように記載されています ( http://dev.mysql.com/doc/refman/5.1/en/group-by-functions.html#function_count ):

COUNT(expr) Returns a count of the number of non-NULL values of expr in the rows retrieved by a SELECT statement

したがって、次のように置き換えます。

IF(sold_price BETWEEN 20 and 50,1,0)

IF(sold_price BETWEEN 20 and 50,1,NULL)
于 2013-03-21T16:09:33.290 に答える