Skip to content

Instantly share code, notes, and snippets.

@avoidwork
Created September 19, 2012 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avoidwork/3749846 to your computer and use it in GitHub Desktop.
Save avoidwork/3749846 to your computer and use it in GitHub Desktop.
MySQL delimited insert
# The second SET statement is optional, and could be ignored if you have a well formed delimited string
SET @var = 'test, w00t, w00tz';
SET @var := REPLACE(@var, ' ', '');
SET @var := CONCAT("('", REPLACE(@var, ",", "'),('"), "')");
SET @sql := CONCAT('INSERT INTO table VALUES ', @var);
# Verify the query, exeute via prepared statement
SELECT @sql AS `query`;
# `query` = INSERT INTO table VALUES ('test'),('w00t'),('w00tz')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment