creating a new column in a MySQL select query

I’ve been really busy since my last post (a whole month without a post!). Work is exceptionally busy and work comes first. Even my reading has slowed down a bit. I have several things waiting to be posted when I get time to write about them – including the first ever competition for this blog! You’ll have to check back over the next week or two to find out what that’s all about, in the mean time this post is all about…

i-049902d1550e456dd21b47b1967a27cb-mysql_100x52-64.gif

“creating a new column in a MySQL select query”

So, here I am manipulating some data from a supplier to make it work with our internal business systems. Essentially I had to work out how much a kitchen cabinet would cost given an “assembly list”, “component list”, “decision list” and of course “cup of coffee”.

Assembly list tells me that each unit needs a cabinet and a door. There are lots of door colours and lots of cabinet colours. These options are all in the component list which tells me the price of each door. The decision list is my own creation and says “build me a unit using a Maple door and a Maple cabinet”. “cup of coffee” feeds my habit while I code. So far, so good.

Once I’ve created a list of units using maple doors and maple cabinets, I then want to create another list of oak doors and oak cabinets. Join them all together and I have a complete list of cabinets that customers can buy. I needed a way of identifying which cabinet was built with which options and for the life of me I couldn’t remember how to do it. I needed to create an additional column in my query that would record what “decision list” row had been used for this build.

The answer is of course obvious once you know it:
Select “StevesDecision” as StevesColumnName, FirstBuildUpStaticTable.Material, SUM(`Gross weight`) As GWeight, and so on.

“StevesDecision” becomes the content of each row and “StevesColumnName” becomes the column name. All the rest of the select… line remains the same.


Comments

2 responses to “creating a new column in a MySQL select query”

  1. socrates

    that statement doesn’t create a new column but only renames a row that you have on the table already!

  2. Hi Socrates, sorry for the time taken to publish your comment.

    Yes, I was a little vague here. Using ‘as’ can rename an existing column, or give a name to the result of a function into a new column. EG: Select `A`,`B`,`A+B` as `C`,…. would create a new column called C in the results which is the sum of a and b.

    This all refers to creating a column in a query – creating a column in a table is different and not related to the trouble I was having.

Leave a Reply

Your email address will not be published. Required fields are marked *

Search this site


Free apps

  • birthday.sroot.eu – Your birthday or other celebration date based on [years on other planets] / [how many seconds/days] / [how far you’ve travelled around the sun]
  • stampulator.sroot.eu – Calculates the combination and how many 1st, 2nd, large 1st and large 2nd class Royal Mail stamps you need on large envelopes and packets

Recent posts


Archives


Categories