D
D
Div-Man2018-10-17 00:04:05
MySQL
Div-Man, 2018-10-17 00:04:05

How to count all fields?

It is necessary to calculate how many masters for each city with one request, why the calculation is not combined and how to do it?

CREATE TABLE `master_user` (
  `id` int(10) UNSIGNED NOT NULL,
  `user_id` int(11) NOT NULL,
  `master_id` int(11) NOT NULL,
  `city_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

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

INSERT INTO `master_user` (`id`, `user_id`, `master_id`, `city_id`) VALUES
(15, 16, 3, 4019),
(18, 16, 5, 4019),
(19, 16, 4, 4019),
(20, 16, 2, 4019),
(21, 17, 4, 4019),
(22, 17, 2, 4400);

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

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

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

--
-- AUTO_INCREMENT для таблицы `master_user`
--
ALTER TABLE `master_user`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=23;
COMMIT;

SELECT master_id, COUNT(*) FROM master_user WHERE city_id = 4019 GROUP BY id

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
nozzy, 2018-10-17
@Div-Man

SELECT master_id, COUNT(*) FROM master_user WHERE city_id = 4019 GROUP BY master_id

D
Dmitry Telepnev, 2018-10-17
@TelepnevDmitriy

If we count the number of masters in cities, then
Select city_id, count(master_id) from master_user group by city_id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question