Can I use a SELECT subquery inside an INSERT query?
I've tried following the syntax suggested here, but it doesn't work for my query.
Here's what I currently have:
INSERT INTO dot (entrant_id, year, entry_code, title, retail_category, campaign_type, client_company, client_city, client_state, field_date_active, height, width, depth, weight, oversize_fee, volt_110, attach_to_gridwall, hang_from_ceiling, table_开发者_开发问答or_countertop, misc_setup, created_date, modified_date, co_entrant)
VALUES (288, 2011, 1234, 'RG Total Defense BW mixed Floorstand', '32', 'C', 'Henkel', 'Scottsdale', 'AZ', '2011-01-15', 60, 26, 15, 29, 0, '0', '0', '0', '0', '', NOW(), NOW(), '')
However, I need the value of entry_code to be selected from the same table - I can do it as an individual query:
MAX(entry_code) FROM dot WHERE year = 2011
but can't seem to integrate that with the insert statement. Is this possible? Or will a subquery like that only work if it's selecting from a different table?
INSERT INTO dot (entrant_id, year, entry_code, ...)
SELECT 288, 2011, MAX(entry_code), ... FROM dot WHERE year=2011
The syntax in the link you provided should work. But that's not the syntax you "currently have" try syntax like this (which is quite similar to that link..):
insert into dot(field1, field2, field3, field4) select max(entry_code), 'value2', val3, 'value4' from dot where year = 2011
INSERT INTO dot (entrant_id, year, entry_code, title, retail_category, campaign_type, client_company, client_city, client_state, field_date_active, height, width, depth, weight, oversize_fee, volt_110, attach_to_gridwall, hang_from_ceiling, table_or_countertop, misc_setup, created_date, modified_date, co_entrant)
VALUES (288, 2011, (SELECT MAX(entry_code) FROM dot WHERE year = 2011), 'RG Total Defense BW mixed Floorstand', '32', 'C', 'Henkel' ...
精彩评论