A
A
Alexander Shilov2016-06-05 00:50:10
SQL
Alexander Shilov, 2016-06-05 00:50:10

How to write query with SQL subquery?

The tov_group table is given , where the product groups and their id are registered, the tovar table is also given , where the product id id_group (product group id), name(product name), ed_izm (product units) and cost(product cost) are registered. It is necessary to write such a query that will display a list of units of measurement indicating the number of different groups that contain goods measured in the corresponding units.
So that there is no suspicion that I did not even think, I cite ref. the code. The thing I wrote below is not working correctly. Help fix.

SELECT ED_IZM, (SELECT COUNT(ED_IZM) FROM TOVAR) FROM TOVAR GROUP BY ED_IZM

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
AlikDex, 2016-06-05
@AlikDex

Table:

--
-- Структура таблицы `products`
--

CREATE TABLE IF NOT EXISTS `products` (
  `id` int(11) NOT NULL,
  `id_group` smallint(6) NOT NULL,
  `unit` varchar(20) NOT NULL
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

--
-- Дамп данных таблицы `products`
--

INSERT INTO `products` (`id`, `id_group`, `unit`) VALUES
(1, 1, 'метры'),
(2, 2, 'литры'),
(3, 1, 'килограммы'),
(4, 1, 'метры'),
(5, 1, 'литры'),
(6, 2, 'килограммы'),
(7, 1, 'килограммы');

--
-- Индексы сохранённых таблиц
--

--
-- Индексы таблицы `products`
--
ALTER TABLE `products`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT для сохранённых таблиц
--

--
-- AUTO_INCREMENT для таблицы `products`
--
ALTER TABLE `products`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=8;

Query without joins:
SELECT `unit`, COUNT(DISTINCT `id_group`) 
FROM `products`  
GROUP BY `unit`

I
iegor, 2016-06-05
@iegor

So join and count product IDs

K
kalapanga, 2016-06-05
@kalapanga

Solutions:
1 - Correct - go to your teacher for a consultation / additional lesson
2 - Wrong, but also working - Pay money to a friend / acquaintance / classmate / freelancer for the solution
Or they tell you JOIN, but you don’t even know the word. And it seems to me that they should know.

V
Vitaly, 2016-06-05
@vshvydky

in our time, perseverance was stronger, no one wrote on the forums how to solve the problem on the material being passed

SELECT
COUNT(t1.ed_izm), t1.ed_izm 
FROM 
(SELECT DISTINCT t.ed_izm, t.id_group FROM tovar t) AS t1 
GROUP BY t1.ed_izm

Result:
1 kg
1 set
2 pcs
Just in case select * from product
1 1 Spoon pcs 12.00
2 1 Fork pcs 11.00
3 1 Plates set 400.00
4 1 Pots set 500.00
5 1 Teapots set 300.00
6 2 Carrots kg 10.00
7 2 Potatoes kg 13.00
8 2 Beetroot kg 17.00
9 2 Radish kg 9.00
10 2 Lemon pcs 5.00
11 2 Orange pcs 8.00
product table is
CREATE TABLE test.tovar (
  id int(11) NOT NULL AUTO_INCREMENT,
  id_group int(11) DEFAULT NULL,
  name varchar(50) DEFAULT NULL,
  ed_izm varchar(255) DEFAULT NULL,
  cost decimal(19, 2) DEFAULT NULL,
  PRIMARY KEY (id)
)
ENGINE = INNODB
AUTO_INCREMENT = 1
CHARACTER SET utf8
COLLATE utf8_general_ci
ROW_FORMAT = DYNAMIC;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question