2

I have this table:

Teams:
ID | Size1 | Size2 | Size3 | Size4 | ... (other columns)..
----------------------------------------
1  |  L    |  XL   |   L   |   M   | ...
----------------------------------------
2  |  S    |  L    |   S   |  XXL  | ...
----------------------------------------
3  |  M    |  XXL  |   L   |   M   | ...
----------------------------------------
4  |  L    |  XL   |  XS   |  XXL  | ...

What is the most effective (and simplest) MySQL query to count all L in table?

I'd like to have only one field in result which is the count of all L in all columns.

EDIT: Just to clarify little more, in my table there is 152 Ls in first column, 2 in second, 151 in third and 3 in fourth and I expect 308 as result

6
  • 1
    SELECT ( SELECT COUNT(*) FROM Teams WHERE Size1='L' ) + ( SELECT COUNT(*) FROM Teams WHERE Size2='L' ) + ( SELECT COUNT(*) FROM Teams WHERE Size3='L' ) + ( SELECT COUNT(*) FROM Teams WHERE Size4='L' ) + ( ... ); Commented Feb 4, 2014 at 12:45
  • w3schools.com/sql/sql_func_count.asp Commented Feb 4, 2014 at 12:46
  • 1
    consider normalizing size fields to their own table - then only have to do single count on that - and not have to change table if you have another size. Otherwise Alexander's answer is the way to go. Commented Feb 4, 2014 at 12:47
  • @Buksy now you need to check all queries on perfomance, and give the final answer Commented Feb 4, 2014 at 12:51
  • And, btw, if this is the table that shows availability of the clothes of each size, this architecture isn't the best one. Commented Feb 4, 2014 at 12:53

4 Answers 4

4

You can use SUM and CASE to do that:

SELECT 
  sum(case when Size1 = 'L' then 1 else 0 end) +
  sum(case when Size2 = 'L'  then 1 else 0 end) +
  sum(case when Size3 = 'L'  then 1 else 0 end) +
  sum(case when Size4 = 'L'  then 1 else 0 end)
FROM Teams;

Alternatively instead of CASE, you can use IF:

SELECT 
  sum(IF(Size1 = 'L',1, 0)) +
  sum(IF(Size2 = 'L',1, 0)) +
  sum(IF(Size3 = 'L',1, 0)) +
  sum(IF(Size4 = 'L',1, 0))
FROM Teams;

This is actually the same.

Edit.

According to Andomar's comment there's even simpler solution:

SELECT 
  sum(Size1 = 'L') +
  sum(Size2 = 'L') +
  sum(Size3 = 'L') +
  sum(Size4 = 'L')
FROM Teams;

This is correct since true is equal to 1 in MySQL. I've just verified this. ;-)

2nd Edit

Next step to simplify this - only one SUM usage:

   SELECT sum(
      (Size1 = 'L') + 
      (Size2 = 'L') +
      (Size3 = 'L') +
      (Size4 = 'L') )
    FROM Teams;
Sign up to request clarification or add additional context in comments.

2 Comments

Since in MySQL true is 1, a shorter version would be sum(size = 'L'). Example at SQL Fiddle.
you have typo in parentheses in second Query, I cant edit it :)
1

Please try the following query:

SELECT sum( 
  IF(Size1='L', 1, 0) + 
  IF(Size2='L', 1, 0) + 
  IF(Size3='L', 1, 0) + 
  IF(Size4='L', 1, 0)
) as total FROM Teams

Comments

1

Assuming there can only be one 'L' per row:

select  count(*)
from    YourTable
where   'L' in (Size1, Size2, ..., SizeN)

Or a normalizing solution, which supports multiple 'L''s per row:

select  count(*)
from    (
        select  size1 as size
        from    YourTable
        union all 
        select  size2
        from    YourTable
        union all 
        select  size3
        from    YourTable
        union all 
        ...
        )
where   size = 'L'

6 Comments

Can you do value in column like that? That's news to me
@Askanison4: a in (b,c,d) is just syntactic sugar for a = b or a = c or a = d
@dragoste: Correct. I assumed it wouldn't make sense to have two sizes called L for the same product, and the example data agrees with that.
@Andomar we're not here to decide what makes sense in authors task. There are two Ls in first row in provided example data.
The first query doesnt work to me SELECT count( * ) FROM table WHERE 'L' IN (size1, size2, size3, size4) returns 152, while there is 152 Ls in first column, 2 in second, 151 in third and 3 in fourth
|
0

Your WHERE condition is going to be quite large but you could use this:

SELECT count(Size1) + count(size2) AS total WHERE size1 = 'L' OR size2 = 'L'

This solution will count all columns and total based on individual column comparisons.

2 Comments

This should count rows twice even if there is only one L
@Andomar Yes this is his expected result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.