0

皆さん、申し訳ありませんが、これについてはmysql JOINの例をいくつか見てきましたが、動作させることができないようです。

「販売」

----------------------
idcustomer | datecode 
----------------------
 1         | 20120503 
 1         | 20120503 
 1         | 20120503 
 2         | 20120503 
 3         | 20120503 

誰がトップバイヤーであるかを知りたい....顧客が特定の日に私から何かを購入する回数の観点から(はい、私が知っている日付に奇妙な形式を使用しています、それを気にしないでください)...だから私は行う:

SELECT idcustomer, COUNT(idcustomer) FROM sales WHERE datecode = 20120503 GROUP BY idcustomer ORDER BY COUNT(idcustomer) DESC

そして私は得る:

-----------------------------
idcustomer | Count(idcustomer)
-----------------------------
 1         | 3
 2         | 1
 3         | 1

質問は...私もテーブルを持っているので:

"お客様"

----------------------
| name | id_customer |
----------------------
 Jess  | 1
 Matt  | 2
 Perry | 3 

そして、以下は私が達成したいことです....それを行う方法は?

---------------------------------------------
customer.name | idcustomer | Count(idcustomer)
---------------------------------------------
 Jess         | 1          | 3
 Matt         | 2          | 1
 Perry        | 3          | 1
4

4 に答える 4

1
SELECT customer.name, idcustomer, COUNT(idcustomer)
FROM sales
JOIN customer
ON sales.idcustomer = customer.id_customer
WHERE datecode = 20120503
GROUP BY idcustomer
ORDER BY COUNT(idcustomer) DESC

オンラインで動作することを確認してください:sqlfiddle

于 2012-06-06T05:39:59.930 に答える
0

あなたはそれをこのようにする必要があるでしょう-

SELECT idcustomer, c.name, COUNT(idcustomer) 
FROM sales s inner join customer c on (s.idcoustomer=c.id_customer)  
WHERE datecode = 20120503 
GROUP BY idcustomer, c.name  
ORDER BY COUNT(idcustomer) DESC
于 2012-06-06T05:42:55.837 に答える
0

これがお役に立てば幸いです::

Select cust_ref.idcustomer, cust_ref.COUNT(idcustomer), customer.name
from 
(SELECT idcustomer, COUNT(idcustomer) FROM sales WHERE datecode = 20120503 
 GROUP BY idcustomer) cust_ref
 inner join customer on (sales.idcustomer = customer.id_customer)
ORDER BY COUNT(idcustomer) DESC
于 2012-06-06T05:53:10.410 に答える
0
SELECT 
  t1.name,t2.count_buy,t2.idcustomer 
   FROM customer as t1,
 (SELECT 
    idcustomer,COUNT(idcustomer) as count_buy 
       FROM `sales`  
        WHERE datecode = 20120503
        GROUP BY idcustomer 
         ORDER BY COUNT(idcustomer) DESC) t2
  WHERE 
   t1.idcustomer =t2.idcustomer
于 2012-06-06T06:01:50.153 に答える