X
X
XenK2016-08-11 08:32:01
PHP
XenK, 2016-08-11 08:32:01

WHERE in SELECT SQL?

There is a request like this:

SELECT `money` FROM `table`  WHERE date(`date`) = '2016-08-11'

It is necessary to display in the same query a column where, for example, the amount for the previous day will be displayed.
How to do it without union, i.e. by adding a condition to SELECT?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey, 2016-08-11
@k1lex

SELECT [Поле группировки], sum(что сложить) FROM `table`  
 WHERE <условие>
 group by [Поле группировки]

or
SELECT [Поле группировки], sum(что сложить) FROM `table`  
 group by [Поле группировки]
having [условие]

Requests work in much the same way. Only in the first one, we first impose a condition on the table, and then group. And in the second, we first get the data and impose a condition.

S
SharuPoNemnogu, 2016-08-11
@SharuPoNemnogu

SELECT `money` FROM `table`
  WHERE date(`date`) BETWEEN вчера AND сегодня

K
Konstantin Tsvetkov, 2016-08-11
@tsklab

SELECT `money` AS Today, CAST( NULL AS MONEY) AS Yesterday FROM `table`  WHERE date(`date`) = '2016-08-11'
UNION
SELECT NULL, `money` FROM `table`  WHERE date(`date`) = '2016-08-10'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question