8

I am wanting to move items from one table into another using MySQL.

This is what I currently use:

INSERT INTO items_rooms (item_id, room_id,x,y,n)  
SELECT id, room_id,x,y,z
  FROM `items_phoenix`

This works, however I am wanting to insert multiple values into one column instead of each values in different columns.

For example:

In table items_rooms, I would like values x and y from items_phoenix to be placed into one column in items_rooms.

If x = 5 and y = 2, can I save it into items_rooms like this: one column: 5.2 instead of different columns for each values?

Sorry if this is confusing!

5 Answers 5

15

You can use expressions in your SELECT column-list. As long as the columns in the select-list and the columns in the destination table match in number and data type, you can do something like this:

INSERT INTO items_rooms (item_id, room_id, x_dot_y, n)  
SELECT id, room_id, CONCAT(x,'.',y), z
  FROM `items_phoenix`
Sign up to request clarification or add additional context in comments.

1 Comment

This is possible because MySQL doesn't care about the type specified and will convert accordingly.
0

You definitely can do this. See the helpful answers on this. However, you should consider whether this is a good idea. Once you denormalize distinct columns into some kind of string expression in one column, you've created something that is very difficult to maintain and also to query against. You are making your data less usable to yourself in doing this.

1 Comment

@Joseph: Hard to say on this one, for me. I considered making it a comment. But then I felt that the guidance and mentoring aspect of SO was also important to the question in this case.
0

You can use a concatenation for a simple case, e.g:

INSERT INTO items_rooms (
  item_id,
  room_id,
  x_and_y,
  n)
SELECT
  id,
  room_id,
  CONCAT(x, '.', y),
  z
FROM `items_phoenix`

2 Comments

Microsoft SQL Server uses + for string concatenation, but MySQL doesn't.
Not a MySQL expert, so originally had SQL Server syntax (@Zaffy, I did have + "." + in the original edit, so it wasn't arithmetic addition). Edited to reflect correct MySQL syntax.
0

Yes, if the column can store text information

Just CONCAT or CONCAT_WS the values

INSERT INTO items_rooms ( item_id, room_id, my_value, n )
SELECT id, room_id, CONCAT_WS( '.', x, y ), z
FROM `items_phoenix`

SQL Fiddle DEMO

Comments

-2
$table_To_Get_From = mysql_query("SELECT * FROM items_phoenix (id, room_id,x,y,z)");
if($table_To_Get_From){
    $table = mysql_fetch_assoc($table_To_Get_From);
    $values = array($table['id'],$table['room_id'],$table['x'],$table['y'],$table['z']);
    mysql_query("INSERT INTO items_rooms (item_id, room_id,x,y,n)  
        VALUES ($values[0],$values[1],$values[2],$values[3],$value[4])");
}

I didn't make them strings, so you may need to use '$values[num]'

This will insert it like you wanted, if there is any values.

1 Comment

how do you know the OP can use PHP? What if he uses Delphi, C++, or whatever. The question is only about MySQL

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.