brandy
Новичок
3 таблицы. Сложная выборка, как сделать?
Пример.
users
+--------------+--------------------------------+
|userID -------| userName ----------------------|
+--------------+--------------------------------+
|-- 1 ---------| Name 1 ------------------------|
+--------------+--------------------------------+
|-- 2 ---------| Name 2 ------------------------|
+--------------+--------------------------------+
products
+--------------+--------------------------------+----------------+
|-- prodID ----| prodName ----------------------|--userID--------+
+--------------+--------------------------------+----------------+
|-- 1 ---------| Name1 -------------------------|--1-------------+
+--------------+--------------------------------+----------------+
|-- 2 ---------| Name2 -------------------------|--1-------------+
+--------------+--------------------------------+----------------+
|-- 3 ---------| Name3 -------------------------|--1-------------+
+--------------+--------------------------------+----------------+
|-- 4 ---------| Name4 -------------------------|--2-------------+
+--------------+--------------------------------+----------------+
votes
+--------------+--------------------------------+---------------------------+
|voteID -------| prodID ------------------------| mark ---------------------|
+--------------+--------------------------------+---------------------------+
|-- 1 ---------| 1 -----------------------------| 5 ------------------------|
+--------------+--------------------------------+---------------------------+
|-- 2 ---------| 1 -----------------------------| 4 ------------------------|
+--------------+--------------------------------+---------------------------+
|-- 3 ---------| 2 -----------------------------| 3 ------------------------|
+--------------+--------------------------------+---------------------------+
|-- 4 ---------| 2 -----------------------------| 3 ------------------------|
+--------------+--------------------------------+---------------------------+
|-- 5 ---------| 4 -----------------------------| 4 ------------------------|
+--------------+--------------------------------+---------------------------+
|-- 6 ---------| 4 -----------------------------| 3 ------------------------|
+--------------+--------------------------------+---------------------------+
|-- 7 ---------| 4 -----------------------------| 4 ------------------------|
+--------------+--------------------------------+---------------------------+
нужна такая выборка, которая вернет всех пользователей, количество добавленных ими продуктов, за которые проголосовали минимум 2 раза.
+--------------+--------------------------------+------------+
|userID -------| userName ----------------------| cnt -------|
+--------------+--------------------------------+------------+
|-- 1 ---------| Name 1 ------------------------| 2 ---------|
+--------------+--------------------------------+------------+
|-- 2 ---------| Name 2 ------------------------| 1 ---------|
+--------------+--------------------------------+------------+
Сам смог сделать почти такой запрос
select u.*, p.prodID
from users u
left join products p on u.userID=p.userID
left join votes as v on p.prodID=v.prodID
group by p.prodID having count(voteID)>=2
который возвращает
+--------------+--------------------------------+------------+
|userID -------| userName ----------------------| prodID ----|
+--------------+--------------------------------+------------+
|-- 1 ---------| Name 1 ------------------------| 1 ---------|
+--------------+--------------------------------+------------+
|-- 1 ---------| Name 1 ------------------------| 2 ---------|
+--------------+--------------------------------+------------+
|-- 2 ---------| Name 2 ------------------------| 4 ---------|
+--------------+--------------------------------+------------+
Как теперь посчитать количество - не знаю
Заранее спасибо
Пример.
users
+--------------+--------------------------------+
|userID -------| userName ----------------------|
+--------------+--------------------------------+
|-- 1 ---------| Name 1 ------------------------|
+--------------+--------------------------------+
|-- 2 ---------| Name 2 ------------------------|
+--------------+--------------------------------+
products
+--------------+--------------------------------+----------------+
|-- prodID ----| prodName ----------------------|--userID--------+
+--------------+--------------------------------+----------------+
|-- 1 ---------| Name1 -------------------------|--1-------------+
+--------------+--------------------------------+----------------+
|-- 2 ---------| Name2 -------------------------|--1-------------+
+--------------+--------------------------------+----------------+
|-- 3 ---------| Name3 -------------------------|--1-------------+
+--------------+--------------------------------+----------------+
|-- 4 ---------| Name4 -------------------------|--2-------------+
+--------------+--------------------------------+----------------+
votes
+--------------+--------------------------------+---------------------------+
|voteID -------| prodID ------------------------| mark ---------------------|
+--------------+--------------------------------+---------------------------+
|-- 1 ---------| 1 -----------------------------| 5 ------------------------|
+--------------+--------------------------------+---------------------------+
|-- 2 ---------| 1 -----------------------------| 4 ------------------------|
+--------------+--------------------------------+---------------------------+
|-- 3 ---------| 2 -----------------------------| 3 ------------------------|
+--------------+--------------------------------+---------------------------+
|-- 4 ---------| 2 -----------------------------| 3 ------------------------|
+--------------+--------------------------------+---------------------------+
|-- 5 ---------| 4 -----------------------------| 4 ------------------------|
+--------------+--------------------------------+---------------------------+
|-- 6 ---------| 4 -----------------------------| 3 ------------------------|
+--------------+--------------------------------+---------------------------+
|-- 7 ---------| 4 -----------------------------| 4 ------------------------|
+--------------+--------------------------------+---------------------------+
нужна такая выборка, которая вернет всех пользователей, количество добавленных ими продуктов, за которые проголосовали минимум 2 раза.
+--------------+--------------------------------+------------+
|userID -------| userName ----------------------| cnt -------|
+--------------+--------------------------------+------------+
|-- 1 ---------| Name 1 ------------------------| 2 ---------|
+--------------+--------------------------------+------------+
|-- 2 ---------| Name 2 ------------------------| 1 ---------|
+--------------+--------------------------------+------------+
Сам смог сделать почти такой запрос
select u.*, p.prodID
from users u
left join products p on u.userID=p.userID
left join votes as v on p.prodID=v.prodID
group by p.prodID having count(voteID)>=2
который возвращает
+--------------+--------------------------------+------------+
|userID -------| userName ----------------------| prodID ----|
+--------------+--------------------------------+------------+
|-- 1 ---------| Name 1 ------------------------| 1 ---------|
+--------------+--------------------------------+------------+
|-- 1 ---------| Name 1 ------------------------| 2 ---------|
+--------------+--------------------------------+------------+
|-- 2 ---------| Name 2 ------------------------| 4 ---------|
+--------------+--------------------------------+------------+
Как теперь посчитать количество - не знаю
Заранее спасибо