T
T
tempick2020-04-25 09:07:08
MySQL
tempick, 2020-04-25 09:07:08

How to make a selection with the addition of a field and its value by condition?

There is a table from several fields. I have a request that looks like this:

SELECT id,fd,date,sum(sum) as spending FROM `spending` GROUP BY fd


There may be several entries with the same fd, they are grouped. In this case, there is a field "product_id", which can be NULL or a number.

How to make one more field (... as is_empty) when outputting, which will be equal to "1" if there is at least one product_id=NULL value among records with the same "fd"? And is it even possible?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2020-04-25
@tempick

COUNT(`product_id`) != COUNT(*) AS `is_empty`
And why do you need a selection of `id` and `date`? MySQL, of course, allows such a departure from the standard, but think about which line, which fell into the group by `fd`, should the values ​​of these fields be selected from?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question