How to select all but one specific column?

12

You can select all columns in a table:

SELECT * FROM wp_rw_programacao WHERE id = $id

But I need to eliminate the id column from the result. Is it possible?

    
asked by anonymous 19.10.2017 / 17:09

3 answers

9

You can not get all of the columns except for one or a few specific columns.

Generally getting everyone is already a bug , it gets worse when you have exceptions.

Almost always someone wants to do this is to type less. This is not appropriate.

If you want to get a specific list you should put them all together. If you want everyone, that is, if you change whether the different columns are changed in the query automatically, it fits * . It's almost never what the person wants.

Make the correct semantic form you need.

If you really want the total column list to come, less one would have to handle it outside of SQL.

There is even an automated solution where you can read the MySQL data about this table and generate the query from this, but you should only do it if you really need the query be dynamic according to the current structure.

You have an example of how to do this in the Wictor Keys answer (but note that it is easy to have security problems if you abuse this and you are not sure what you are doing, especially if that $id comes from an external source). But do not do this to save typing, your code will be semantically wrong if it's not what you need and even if it does not bring problems now it will bring you into the future.

To make this work the whole system needs to be done thinking about it.

    
19.10.2017 / 17:18
7

This way you can:

SET @sql = CONCAT('SELECT ', (SELECT REPLACE(GROUP_CONCAT(COLUMN_NAME), '<columns_to_omit>,', '') FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '<table>' AND TABLE_SCHEMA = '<database>'), ' FROM <table>');

PREPARE stmt1 FROM @sql;
EXECUTE stmt1;

Replace <table>, <database> e <columns_to_omit>

source: link

    
19.10.2017 / 17:17
6

You can call the fields you want by doing so, for example:

SELECT campo1, campo2, campo3, campo4 
FROM wp_rw_programacao 
WHERE id = $id

The only fields that will be returned are: campo1, campo2, campo3, campo4

  

More information .

There is no other way to be done. See here for more information on StackOverflow en .

    
19.10.2017 / 17:13