Question

I am trying to count the number of times that a word appears in all the columns of a row, done an id.

+-------+------+------+------+------+------+------+
| id    | pos1 | pos2 | pos3 | pos4 | pos5 | pos6 |
+-------+------+------+------+------+------+------+
| core1 | AA   | BB   | CC   | HH   | YY   | var1 |
| core2 | AA   | BB   | var3 | TT   | var2 | YY   |
| core5 | AA   | BB   | EE   | GG   | YY   | ZZ   |
+-------+------+------+------+------+------+------+

I've tried with this code but only returns '1' and not '2' that is what I want to.

SELECT count(id) FROM customize WHERE pos1 OR pos2 OR pos3 OR pos4 OR pos5 OR pos6 LIKE 'var%' AND id = 'core2';

Any ideas of how could I do it?

Was it helpful?

Solution

SELECT 
    (IF(pos1 LIKE 'var%', 1, 0) +
    IF(pos2 LIKE 'var%', 1, 0) +
    IF(pos3 LIKE 'var%', 1, 0) +
    IF(pos4 LIKE 'var%', 1, 0) +
    IF(pos5 LIKE 'var%', 1, 0) +
    IF(pos6 LIKE 'var%', 1, 0)) AS done
FROM customize
WHERE id = 'core2'

OTHER TIPS

Maybe do a load of unioned queries (one for each column) and then count the results.

SELECT COUNT(*) 
FROM
(
    SELECT id
    FROM customize 
    WHERE id = 'core2'
    AND pos1 LIKE 'var%'
    UNION ALL
    SELECT id
    FROM customize 
    WHERE id = 'core2'
    AND pos2 LIKE 'var%'
    UNION ALL
    SELECT id
    FROM customize 
    WHERE id = 'core2'
    AND pos3 LIKE 'var%'
    UNION ALL
    SELECT id
    FROM customize 
    WHERE id = 'core2'
    AND pos4 LIKE 'var%'
    UNION ALL
    SELECT id
    FROM customize 
    WHERE id = 'core2'
    AND pos5 LIKE 'var%'
    UNION ALL
    SELECT id
    FROM customize 
    WHERE id = 'core2'
    AND pos6 LIKE 'var%'
)

Or if you want a count of the rows that have a column with that value:-

SELECT COUNT(id)
FROM customize 
WHERE id = 'core2'
AND (pos1 LIKE 'var%' OR pos2 LIKE 'var%' OR pos3 LIKE 'var%' OR pos4 LIKE 'var%' OR pos5 LIKE 'var%' OR pos6 LIKE 'var%')

But better to normalise your database design

I think you might want something more like this:

    select 
     IF(Pos1 LIKE 'var%', 1, 0) +
     IF(Pos2 LIKE 'var%', 1, 0) +
     IF(Pos3 LIKE 'var%', 1, 0) +
     IF(Pos4 LIKE 'var%', 1, 0) +
     IF(Pos5 LIKE 'var%', 1, 0) +
     IF(Pos6 LIKE 'var%', 1, 0) 
 FROM customize
    where id = 'core2'

It always helps to try to break down a problem conceptually. There are two stages to solving this problem. The first is to pivot your input table, converting the columns into rows.

The second is to count the values of the strings.

Here is a query to do both (http://sqlfiddle.com/#!2/3731e/7/0).

SELECT COUNT(*) AS stringcount, pos AS string
FROM (  -- pivot the original table
                  SELECT id, 1 AS i, pos1 AS Pos FROM customize
        UNION ALL SELECT id, 2 AS i, pos2 AS Pos FROM customize
        UNION ALL SELECT id, 3 AS i, pos3 AS Pos FROM customize
        UNION ALL SELECT id, 4 AS i, pos4 AS Pos FROM customize
        UNION ALL SELECT id, 5 AS i, pos5 AS Pos FROM customize
        UNION ALL SELECT id, 6 AS i, pos6 AS Pos FROM customize
) AS pivoted
GROUP BY pos
ORDER BY COUNT(*) DESC, pos
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top