0

I need to make a MySQL query where I don't want to select a column named description. Is there an easy way to specifically say to NOT select the column.

Example: I want to SELECT all but the description column. Can this be done or I have to write each one I need to pick instead?

id| Serverid | date_added | description ...
 1        yes   12.03.09     bad engine
 3        yes   12.04.09     ok engine
 2        no    12.05.09     ok engine
 4        yes   12.06.09     bad engine



$cars = lib::$db->GetAll("SELECT SQL_CALC_FOUND_ROWS
                c.*,
            FROM cars AS c
            ORDER c.date_added DESC
            LIMIT 10);
Dharman
  • 30,962
  • 25
  • 85
  • 135
mk81
  • 13
  • 9

2 Answers2

0
SET @sql = CONCAT('SELECT ', (SELECT REPLACE(GROUP_CONCAT(COLUMN1), '<Col To Omit>,', '') FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '<TableName>' AND TABLE_SCHEMA = '<DBName>'), ' FROM <TableName>');

PREPARE stmt1 FROM @sql;
EXECUTE stmt1;
Dharman
  • 30,962
  • 25
  • 85
  • 135
Shantun Parmar
  • 432
  • 2
  • 13
-2

No, there is no sane way to do that except listing all fields manually, like

SELECT id, Serverid, date_added FROM table

On a side note, with any sensible setup, there would be no difference in performance if you do SELECT *

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • 1
    If `description` contains a gigabyte of data per row, there will definitely be a difference in performance. – AKX Mar 24 '20 at 12:25
  • 1
    "with any *sensible* setup" – Your Common Sense Mar 24 '20 at 12:27
  • It's quite possible that the column contains a lot of data, which is why OP wants to elide it. – AKX Mar 24 '20 at 12:30
  • It makes a difference when using column types like text who's data is stored internally in an extra table requiring doing more scans! – Markus Zeller Jul 18 '22 at 11:12
  • @YourCommonSense It is extra work for the database and depending on the indexes, where conditions and table size it can make significant differences in performance. Using a Select * is more work than specifying single columns. – Markus Zeller Jul 18 '22 at 11:42