Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select all columns except one in MySQL?

I'm trying to use a select statement to get all of the columns from a certain MySQL table except one. Is there a simple way to do this?

EDIT: There are 53 columns in this table (NOT MY DESIGN)

like image 844
Tom Grochowicz Avatar asked Aug 12 '08 18:08

Tom Grochowicz


People also ask

How do I select all except one row in SQL?

The SQL EXCEPT operator is used to return all rows in the first SELECT statement that are not returned by the second SELECT statement. Each SELECT statement will define a dataset. The EXCEPT operator will retrieve all records from the first dataset and then remove from the results all records from the second dataset.

How do I exclude something in MySQL?

Write a single MySQL query to exclude a record and display NULL value. To check records which are NULL, use IS NULL. However, to exclude any of the records, use the NOT IN clause. Use both of them in the same query.


1 Answers

Actually there is a way, you need to have permissions of course for doing this ...

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; 

Replacing <table>, <database> and <columns_to_omit>

like image 57
Mahomedalid Avatar answered Sep 23 '22 13:09

Mahomedalid