Answer the question
In order to leave comments, you need to log in
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);
SELECT COUNT(*)/2 FROM `work`......
Answer the question
In order to leave comments, you need to log in
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 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 questionAsk a Question
731 491 924 answers to any question