在mysql中选择除一个字段之外的所有字段

2021-12-19 00:00:00 database select php mysql zend-framework

可能重复:
选择 MySQL 中除一列之外的所有列?

我想知道有没有办法从我的数据库表中选择除一个字段之外的所有字段.

I want to know is there a way to select all fields except one field from a table in my database.

我知道我可以描述选择查询中的字段名称.
例如:

I know I can describe the field names in the select query.
For example:

SELECT fieldname1, fieldname2, fieldname3, fieldname4 FROM tablename;

但我的问题是,有没有办法以简单的方式做到这一点......像这样

But my question is, is there any way to do it in a simple way... Like this

SELECT * FROM tablename EXCEPT(fieldname3);

我正在使用 MySQL 和 Zend 框架.

I am using MySQL and Zend framework.

推荐答案

你可以轻松搞定

假设您的字段是 id = 5

lets say your field is an id = 5

然后

   select * from your_table where id !=5 

如果你的意思是列

假设您不想选择 column3

然后

   select column1,column2,column4 from tablename;

如果你有很多列

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

    PREPARE stmt1 FROM @sql;
   EXECUTE stmt1;

相关文章