Jump to content
  • 0

Не могу составить SQL запрос


solovin1986
 Share

Question

Нужно составить запрос в котором будет 2 подсчета товаров по региону и всего(то есть не учитывая регион)

То что я сделал считает без выборки региона.

Могу сделать 2-мя запросами но для общего развития нужно 1-м

Запрос

SELECT product.id, product.name, count( product.id ) AS all_region
FROM product
JOIN user_product ON product.id = user_product.product_id
WHERE product.id
IN ( 1, 2 )
GROUP BY product.id

База

CREATE TABLE IF NOT EXISTS `product` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=cp1251 AUTO_INCREMENT=4 ;

--
-- Dumping data for table `product`
--

INSERT INTO `product` (`id`, `name`) VALUES
(1, 'Шины'),
(2, 'Диски'),
(3, 'И тд');

-- ——————————————————--

--
-- Table structure for table `region`
--

CREATE TABLE IF NOT EXISTS `region` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=cp1251 AUTO_INCREMENT=4 ;

--
-- Dumping data for table `region`
--

INSERT INTO `region` (`id`, `name`) VALUES
(1, 'Москва'),
(2, 'Минск'),
(3, 'Уфа');

-- ——————————————————--

--
-- Table structure for table `user`
--

CREATE TABLE IF NOT EXISTS `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(40) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=cp1251 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `user`
--

INSERT INTO `user` (`id`, `name`) VALUES
(1, 'Рома'),
(2, 'Вася');

-- ——————————————————--

--
-- Table structure for table `user_product`
--

CREATE TABLE IF NOT EXISTS `user_product` (
`user_id` int(11) NOT NULL,
`product_id` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=cp1251;

--
-- Dumping data for table `user_product`
--

INSERT INTO `user_product` (`user_id`, `product_id`) VALUES
(1, 1),
(1, 2),
(2, 1),
(2, 2);

-- ——————————————————--

--
-- Table structure for table `user_region`
--

CREATE TABLE IF NOT EXISTS `user_region` (
`user_id` int(11) NOT NULL,
`region_id` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=cp1251;

--
-- Dumping data for table `user_region`
--

INSERT INTO `user_region` (`user_id`, `region_id`) VALUES
(1, 1),
(1, 2),
(2, 1);

Edited by solovin1986
Link to comment
Share on other sites

4 answers to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. See more about our Guidelines and Privacy Policy