53

I have the following structure with a MySQL table:

+----------------+----------------+----------+
|    zipcode     |      city      |   state  |
+----------------+----------------+----------+
|     10954      |     Nanuet     |    NY    |
+----------------+----------------+----------+

I want to combine the above 3 columns into one column like this:

+---------------------+
|      combined       |
+---------------------+
| 10954 - Nanuet, NY  |
+---------------------+

And I want to add this "combined" column to the end of the table without destroying the original 3 fields.

4 Answers 4

135

Create the column:

ALTER TABLE yourtable ADD COLUMN combined VARCHAR(50);

Update the current values:

UPDATE yourtable SET combined = CONCAT(zipcode, ' - ', city, ', ', state);

Update all future values automatically:

CREATE TRIGGER insert_trigger
BEFORE INSERT ON yourtable
FOR EACH ROW
SET new.combined = CONCAT(new.zipcode, ' - ', new.city, ', ', new.state);

CREATE TRIGGER update_trigger
BEFORE UPDATE ON yourtable
FOR EACH ROW
SET new.combined = CONCAT(new.zipcode, ' - ', new.city, ', ', new.state);
6
  • How do I set two fields on a update, I tried adding another set new.rowname statement but just get an error.
    – LukeS
    Commented Mar 7, 2013 at 23:08
  • I'm going from memory here, but I'm pretty sure you can just separate the values to set by commas like: set new.foo = 1, new.bar = 2
    – squawknull
    Commented Mar 8, 2013 at 3:37
  • I was wondering what if one field is to come from a different table, i.e. city coming from table2?? Commented May 21, 2013 at 2:37
  • You can query another table in a subquery from a trigger, something like set newLearn more….combined = (select city from foo where ...). However, you should use great caution when building dependencies across tables in triggers. This can cause some really nasty locking issues because any update to one table may require locking multiple other tables. In most cases, you can find a better way, perhaps by either normalizing the data structure better or pushing some of the logic up into your application code rather than pushing it down into the database.
    – squawknull
    Commented May 21, 2013 at 14:48
  • 1
    In most cases, it seems more optimal to concat the data on the fly, rather than creating a new column instance. See MikeTheReader's answer.
    – Clayton
    Commented Jul 27, 2015 at 0:23
27

Are you sure you want to do this? In essence, you're duplicating the data that is in the three original columns. From that point on, you'll need to make sure that the data in the combined field matches the data in the first three columns. This is more overhead for your application, and other processes that update the system will need to understand the relationship.

If you need the data, why not select in when you need it? The SQL for selecting what would be in that field would be:

SELECT CONCAT(zipcode, ' - ', city, ', ', state) FROM Table;

This way, if the data in the fields changes, you don't have to update your combined field.

3
  • I'm using an AutoComplete function and I want to be able to search AutoComplete with both zipcode and city, this seems like the only easy way. At least, with my level of knowledge.
    – stewart715
    Commented Apr 25, 2011 at 2:04
  • 1
    Do your autocomplete like: SELECT * from table where zipcode LIKE '%q% OR city LIKE '%q% or state LIKE '%q%
    – Brian Pipa
    Commented Jan 15, 2013 at 20:10
  • This feels the cleanest to me. Duplicating data and then having to check they're in sync, and instituting additional logic to solve out-of-sync issues in higher-level code seems much dirtier than simply concatenating upon SELECT or even better using the app to concat it. Couldn't agree more with this answer, though @squawknull's answer introduced a cool concept, where appropriate.
    – mrClean
    Commented Dec 6, 2016 at 20:44
16

Add new column to your table and perfrom the query:

UPDATE tbl SET combined = CONCAT(zipcode, ' - ', city, ', ', state)
1
  • I like this option because it's performance over storage. For storage over performance, you could add the concat to the select query itself. See squaknull's post for the trigger code. Commented Apr 25, 2011 at 2:02
0
SELECT CONCAT (zipcode, ' - ', city, ', ', state) AS COMBINED FROM TABLE

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.