php 选择mysql中除一个字段外的所有字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14253994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selecting all fields except only one field in mysql
提问by Deepu
Possible duplicate:
Select all columns except one in MySQL?
可能重复:
选择 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);
I am using MySQL and Zend framework.
我正在使用 MySQL 和 Zend 框架。
采纳答案by echo_Me
you can do it easily like that
你可以像那样轻松做到
lets say your field is an id = 5
假设您的字段是 id = 5
then
然后
select * from your_table where id !=5
and if you mean columns
如果你的意思是列
lets say you dont want select column3
让我们说你不想选择 column3
then
然后
select column1,column2,column4 from tablename;
if you have many columns
如果你有很多列
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;
回答by mbharanidharan88
Yes you can fetch from information_schema.columns
是的,你可以从 information_schema.columns
SET @sql = CONCAT('SELECT ', (SELECT GROUP_CONCAT(COLUMN_NAME) FROM
information_schema.columns WHERE table_schema = 'dbo' AND table_name =
'tablename' AND column_name NOT IN ('c1', 'c2')),
' from dbo.tablename');
PREPARE stmt1 FROM @sql;
EXECUTE stmt1;
回答by Mithun Sen
Try this -
尝试这个 -
SHOW FIELDS FROM `tablename` WHERE FIELD NOT IN ('f1','f2','f3');
Execute this query and fetch the specific field-names and put each field-name into an array. then implode the array with ',' inside the select query.
执行此查询并获取特定的字段名并将每个字段名放入一个数组中。然后在 select 查询中用 ',' 内爆数组。
$fields = implode(',',$fields_arr);
$sql = SELECT $fields FROM `tablename`;

