Search This Blog

Friday, June 24, 2011

MySQL Insert statement with nested CASE and SELECT

The MySQL manual is a little bit confusing so I just wanted to give an example of MySQL’s insert statement with nested CASE and SELECT.

INSERT INTO TEST (startdate, testname, ipaddress, hostname, customerid, application)
  SELECT NOW(),'TEMP_TESTNAME', '192.168.1.1', 'TEMP_HOST',
  CASE count(id)
      WHEN 1 THEN id
      ELSE (SELECT id FROM customer where customername like '%OTHER%')
  END
  , 'MARKET_XXX'
  FROM customer WHERE CUSTOMERNAME like '%XXX%';

1 comment:

If you like this post, please leave a comment :)