A
A
aarty202017-01-06 17:20:20
MySQL
aarty20, 2017-01-06 17:20:20

How to use count/sum with double condition?

The task is to count the number of ordered goods by country of order.
There is a table:
ID | item_name | meta_value
The meta_value contains various product characteristics, including the country and the number of products in the order.
Without the quantity in the order, I get the data from the database like this (here I get an array of all orders):

SELECT
meta_value AS country,
count(case when item_name = '%ITEM_NAME_1%' then 1 else null end) AS count_1
count(case when item_name = '%ITEM_NAME_2%' then 1 else null end) AS count_2
GROUP BY meta_value

But, it counts only by the number of orders, not including the number of custom-made goods in one order. And how to count with two conditions?
As far as I understand, something like this
sum(meta_value.qty case when item_name = '%ITEM_NAME_2%' then 1 else null end)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
aarty20, 2017-01-06
@aarty20

Decided like this:

sum(case when item_name = '%ITEM_NAME_1%' then meta_value else 0 end) AS count_1,
sum(case when item_name = '%ITEM_NAME_2%' then meta_value else 0 end) AS count_2

in meta_value lies just the number of ordered goods in the order.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question