Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting all fields except only one field in mysql [duplicate]

Possible duplicate:
Select all columns except one in 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.

like image 798
Deepu Avatar asked Jan 10 '13 08:01

Deepu


People also ask

How do you select all values from a table only once if they're duplicated?

You can use distinct keyword to select all values from a table only once if they are repeated.

How do I ignore one column in MySQL?

COLUMNS table holds all information about the columns in your MySQL tables. To exclude columns, you use the REPLACE() and GROUP_CONCAT() functions to generate the column names you wish to include in your SELECT statement later.


2 Answers

you can do it easily like that

lets say your field is an id = 5

then

   select * from your_table where id !=5 

and if you mean columns

lets say you dont want select 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;
like image 184
echo_Me Avatar answered Oct 25 '22 11:10

echo_Me


Yes you can fetch from 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;
like image 28
Hary Avatar answered Oct 25 '22 10:10

Hary