0

クライアントが閲覧した製品を含むテーブルがあります。次のようになります。

| id | visitor_id |  product  |

| 1  |   51a474f  |     5     |
| 2  |   51a474f  |     3     |
| 3  |   51a474f  |     3     |
| 4  |   207ed9e  |     3     |
| 4  |   207ed9e  |     5     |
| 5  |   47d98ee  |     4     |
| 6  |   47d98ee  |     3     |
| 7  |   47d98ee  |     2     |

そして、誰かが例えば製品番号を訪問した場合。5、同じ製品を訪問したすべての訪問者を取得し、すべての製品 (ID が 5 以外) をグループ化し、訪問数でカウントしたいと考えています。

複数のクエリでこれを実行できることはわかっていますが、そのソリューションよりも単一のクエリの方が優れていると思います。

4

2 に答える 2

0

あなたの質問から私が理解したことから、これが私の答えです。ただし、データをテストしていません。

select visitor_id,product, count(visitor_id) 
from <your table>
where product=(select product from <your table> where id = (select max(id) from <your table>))
group by product

union

select visitor_id,product, count(visitor_id)
from <your table>
where product not in (select product from <your table> where id = (select max(id) from <your table>))
于 2013-05-28T10:27:03.207 に答える
0

すべての製品 (ID が 5 以外) が必要であり、No. 5? 次のようなことを試すことができます: (頭のてっぺんから、テストされていません)

SELECT product, COUNT(id) num
FROM the_table
WHERE
    visitor_id IN  ( SELECT DISTINCT visitor_id FROM the_table WHERE product='5' AND visitor_id!='this_visitor_id' ) AND
    product != '5'
GROUP BY product
ORDER BY num DESC
于 2013-05-28T10:27:59.390 に答える