Friday, 18 December 2009

A couple of nice MySQL tricks

Do you want to transfer records from one table to another, specifying source and destination columns? Piece of cake:

INSERT INTO destination ( col1 , col2 )
SELECT col3, col4 FROM source


And what if you wish to collapse a subquery in your query results? More complex, but can be easily done with GROUP_CONCAT:

SELECT stuff.., GROUP_CONCAT( column_to_sum_up ) AS newname
FROM main_source
LEFT JOIN other_table_containing_things_to_collapse ON condition
GROUP BY column_in_main_source_that_explains_collapse

2 comments:

Anonymous said...

peccato funzioni solo in mysql, a quanto pare...
bel trucchetto, comunque!

Unknown said...

giusto. l'ho specificato nelle tags ma il titolo รจ fuorviante :)

correggo, grazie della segnalazione