MySQL: Query depending on the value, not the name of the column

0

My question is this:

I have a Mysql table of 500 columns and I want to get all the records whose values are different from 100, and despise the columns with that value. For example:

 Columna1 = 100, Columna2 = 60, Columna3 = 100, Columna4 = 35... Columna  500 = 56

I'm only interested in Columns 2, 4 and 500.

As in the beginning I do not know the value of the registers I can not put: SELECT Columna2, Columna3, Columna500 and since the table is very large I can not put in the WHERE Columna2 <>100 AND Columna3 <>100 and Columna500 <>100 plus each row can be different.

Can you think of a solution?

    
asked by SMPXTR 04.03.2017 в 19:26
source

2 answers

0

As already said, I think you need to think about your table vertically (your 500 columns should actually be 500 rows of a table, with three columns ... or more, but not 500! :) Your question seemed curious to me, so I set out to make a fiddle of a possible solution.

The query will look for any value other than 100 in all rows of your table.

SQL Fiddle

MySQL 5.6 Schema Setup :

CREATE TABLE columnas
    ('id' int, 'columna_num' int, 'valor' int)
;

INSERT INTO columnas
    ('id', 'columna_num', 'valor')
VALUES
    (1,1,100),
    (2,2,60),
    (3,3,100),
    (4,4,35),
    (5,200,100),
    (6,201,100),
    (7,300,100),
    (8,302,100),
    (9,400,100),
    (10,403,100),
    (11,500,56)
;

Query 1 Columns other than 100:

SELECT
  columna_num, valor
FROM
  columnas
WHERE 
  valor <> 100

Results :

| columna_num | valor |
|-------------|-------|
|           2 |    60 |
|           4 |    35 |
|         500 |    56 |

Query 2 All columns up to 500 (test) :

SELECT
  columna_num, valor
FROM
  columnas

Results :

| columna_num | valor |
|-------------|-------|
|           1 |   100 |
|           2 |    60 |
|           3 |   100 |
|           4 |    35 |
|         200 |   100 |
|         201 |   100 |
|         300 |   100 |
|         302 |   100 |
|         400 |   100 |
|         403 |   100 |
|         500 |    56 |
    
answered by 05.03.2017 / 05:01
source
0

Good morning. This more than an answer is an annotation.

If all of your 500 columns use the same type of data, you can change the structure of your table. From what I understand, your table should look like this:

| id |columna 1 | columna 2 | columna 3 | columna 4 | columna 5 | columna 6 | ...
| 1  |1         | 9         | 6         | 3         | 8         | 4         |
| 2  |2         | 4         | 7         | 2         | 6         | 6         |
| 3  |3         | 9         | 5         | 5         | 5         | 9         |
| 4  |6         | 5         | 12        | 7         | 8         | 9         |

You must understand that each row must have a unique identifier. Then you can transform it to a table like the following:

| id | numero_columna | valor
| 1  | 1              | 1
| 1  | 2              | 9
| 1  | 3              | 6
| 1  | 4              | 3
| 2  | 1              | 2
| 2  | 2              | 4
| 2  | 3              | 7
| ...| ...            | ...

Where the primary has the form PRIMARY KEY (id, numero_columna) .

Then your query is like:

SELECT valor FROM tabla WHERE numero_columna in (2, 4, 500) AND valor <> 100;
    
answered by 04.03.2017 в 21:37