SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast ScubaDvr2's Avatar
    Join Date
    Jun 2006
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Getting all but one field in MySQL

    Hey,
    I think it's going to be likely that this isn't possible, but I thought i'd post anyway.

    I want to get all the fields of a db table except one.

    So "SELECT * -thisfield FROM thistable"
    instead of "SELECT * FROM thistable"

    Not possible? Hopefully not.

    Reasoning: I have a table that's going to be changing in the future so I don't want to have to do "SELECT field1, field2, field3" etc and have to update each time the table changes.

  2. #2
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't do it.

    The only way you might be able to do this is use a DESCRIBE table query and use php to come up with the list of fields you want.

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    May I ask why the structure of the table is going to change in the future? Perhaps there's another way of doing what you intend.

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,022
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by ScubaDvr2 View Post
    Reasoning: I have a table that's going to be changing in the future so I don't want to have to do "SELECT field1, field2, field3" etc and have to update each time the table changes.
    this reasoning is misleading, and i'll explain why

    suppose you have an application that needs columns foo and bar from the table

    so you write SELECT foo, bar FROM daTable

    now suppose the table is going to change, by adding a new column, let's call it qux

    okay, now consider all your SELECT statements in the context of what they're doing -- maybe the application module needs the new qux field, maybe it doesn't

    if it does, you have to change the SELECT anyway, and you would do this at the same time as you're changing the application logic to handle the new qux column in that module

    and if the application module doesn't need the new qux field, you won't be changing it, and you won't have to change the SELECT, either

    so, you see, even if the table changes, you'll only have to change your SELECTs if they are actually affected by the change to the table, and then you would ~want~ to change the SELECT, so that it self-documents the columns that that particular application module uses

    make sense?
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •