A
A
Andrey2016-12-18 14:22:18
MySQL
Andrey, 2016-12-18 14:22:18

How to make two selects and calculations in one request?

Good afternoon friends!
Tell me if such a construction is possible in one query:
I need to count all rows with a value greater than 1 in the CODE field, divide their number by 2 (it is always even) and add them to all rows with 0 in the CODE field.
The result of the query should be the number 1 or 2 or 3 and so on.
I write the following structure:

SELECT COUNT(*) FROM `work` WHERE `date_in` BETWEEN (SELECT UNIX_TIMESTAMP(NOW())+ 10740 SECOND) AND (SELECT UNIX_TIMESTAMP(NOW())+ 10800 SECOND) AND `kod`=0 + (SELECT COUNT(*) FROM `work` WHERE `date_in` BETWEEN (SELECT UNIX_TIMESTAMP(NOW())+ 10740 SECOND) AND (SELECT UNIX_TIMESTAMP(NOW())+ 10800 SECOND)) AND `kod`>1 )/2);

But SQLyog swears at the syntax ...
Is it even possible to make such a query?
UPD: I figured out the division
SELECT COUNT(*)/2 FROM `work`......

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey, 2016-12-20
@andrey71

Everything turned out to be easier than I thought:

SELECT(
(SELECT COUNT(1) FROM `work` WHERE `date_in` BETWEEN (SELECT UNIX_TIMESTAMP(NOW())+ 10740 SECOND) AND (SELECT UNIX_TIMESTAMP(NOW())+ 10800 SECOND) AND `kod`=0)
+
(SELECT COUNT(1)/2 FROM `work` WHERE `date_in` BETWEEN (SELECT UNIX_TIMESTAMP(NOW())+ 10740 SECOND) AND (SELECT UNIX_TIMESTAMP(NOW())+ 10800 SECOND) AND `kod`>1)) AS tehn;

Maybe someone will come in handy.
I didn't come to a decision on my own. A friend suggested. Roman, thank you!

M
Max, 2016-12-18
@MaxDukov

maybe so?

SELECT SUM(1) FROM (
SELECT COUNT(*)/2 FROM `work` WHERE `date_in` BETWEEN (SELECT UNIX_TIMESTAMP(NOW())+ 10740 SECOND) AND (SELECT UNIX_TIMESTAMP(NOW())+ 10800 SECOND) AND `kod`=0 
 UNION
(SELECT COUNT(*) FROM `work` WHERE `date_in` BETWEEN (SELECT UNIX_TIMESTAMP(NOW())+ 10740 SECOND) AND (SELECT UNIX_TIMESTAMP(NOW())+ 10800 SECOND)) AND `kod`>1 ))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question