View Full Version : querying for everything except one
gymshoe
08-14-2005, 02:16 AM
I've scoured mysql reference manual online and can't seem to find how to do this.
Let's say I have 26 fields in my database A through Z.
I want to do a query for all the fields except field Z.
I don't want to do this:
SELECT a, b, c, d, e, f, g, h, i, j , ...w, x, y FROM tablename;
there's gotta be an easier way...
-James
sheila
08-14-2005, 11:06 AM
Why not just return all of the columns, and ignore z?
SELECT * FROM tablename;
PaulKroll
08-14-2005, 02:18 PM
there's gotta be an easier way...
Nope. If you can't use Sheila's suggestion, you need to list the individual columns.
Actually it's best if you DO supply precisely the columns you want, from a query, lest you have to add to the table later and end up having to change all your "select *" queries. It's also supposed to be slightly faster, as the db doesn't have to go find out the list of all columns for that table, but without a benchmark, I'd guess that's REALLY slight.
If you need to exclude one column in many situations, either because it's a huge blob field or you have some prewritten code that tries to display everything you get from a query or some other reason, you might code around it. In other words, make a piece of code ask MySQL all the column names for that table, and then build the SELECT string minus the one column you don't want.
If the issue is just that you don't want to type out all of the field names for each query, then one workaround is to put them all into an array (or text string) and use that to assemble the SELECT fields on the fly.
Dan
vBulletin® v3.6.8, Copyright ©2000-2009, Jelsoft Enterprises Ltd.