Is it possible to select all the fields in a database except a few?

0

I've been searching and I can not find it, but I've found that in a mysql query, I need to select almost all the content minus 3 elements, the point is that there are almost 20 columns. I know that php can not show those fields, but the output is made by php and I transform it into a json output and I wanted to save myself a possible php code that can be saved from the mysql base.

I have chosen to select field by field, but Can it be done in a more elegant way?

An example:

SELECT  b.gorro , b.pierna_derecha , b.pierna_izquierda , b.brazo_derecho , b.brazo.izquierdo ,...
FROM batman AS b

In mysql there is a way to select all but a few fields in particular?

    
asked by CodeNoob 04.11.2016 в 17:43
source

3 answers

2

Here there is an answer in English that deals with the same subject. Apparently it can not be done in an elegant way, but rather following a series of "tricks" that for what I saw generate wear and tear on the performance and readability of the query.

My advice: take the time to write the almost 20 columns, both sql and whoever sees the code later will thank you.

    
answered by 04.11.2016 / 17:53
source
0

Really if you can do it. Imagine you have this table created:

CREATE TABLE Persons(
  id int PRIMARY KEY,
  gorro varchar(255),
  pierna_derecha varchar(255),
  pierna_izquierda varchar(255),
  brazo_derecho varchar(255)
);

INSERT INTO Persons VALUES(1,'gorro', 'pierna derecha', 'pierna izq', 'brazo dcho');

And you want to omit the columns pierna_derecha and pierna_izquierda of the table Persons when doing the select. You would do the following:

SET @sql = CONCAT('SELECT ', (SELECT REPLACE(GROUP_CONCAT(COLUMN_NAME), 'pierna_derecha,pierna_izquierda', '') FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Persons' AND TABLE_SCHEMA = 'tuBaseDeDatos'), ' FROM Persons');

PREPARE stmt1 FROM @sql;
EXECUTE stmt1;

Keep in mind that you would have to change tuBaseDeDatos for your actual database.

    
answered by 04.11.2016 в 17:56
0

If that select you want php does not have to think, but simply arrives from the database correctly, one option you have is to use the views. You create a view SELECT * FROM tuTabla once created, mySql will have changed the * for each of the fields, remove the fields you do not want to show.

    
answered by 04.11.2016 в 20:56