I'm new to SQL and have run into a problem. I have searched and read many posts about this error but I can't seem to connect any of the answers to my problem. As far as I can see I'm not doing the same thing, trying to return 2 columns in my SELECT
.
INSERT INTO `countries` (
`alpha2_id`, `alpha3_id`, `numeric_id`, `common_name`,
`formal_name`, `capital`, `type_id`, `subtype_id`,
`un_admission_date`, `eu_accession_date`, `anthem`, `motto`,
`flag_name`, `seal_name`, `coa_name`
)
VALUES (
'AF', 'AFG', '004', 'Afghanistan', 'Islamic State of Afghanistan', 'Kabul',
(
SELECT id_no FROM `country_types`
WHERE `type_id`='T' AND `description`='Independent State'
),
(
SELECT id_no FROM `country_types`
WHERE `type_id`='S' AND `description`=''
),
'1946/11/19', null, 'Afghan National Anthem', '', 'af.png', '', 'af_coa.png'
);
This test query works fine but when I try to add a 2nd line I get the SQL error 1241.
INSERT INTO `countries` (
`alpha2_id`, `alpha3_id`, `numeric_id`, `common_name`,
`formal_name`, `capital`, `type_id`, `subtype_id`,
`un_admission_date`, `eu_accession_date`, `anthem`, `motto`,
`flag_name`, `seal_name`, `coa_name`
)
VALUES (
(
'AF', 'AFG', '004', 'Afghanistan', 'Islamic State of Afghanistan', 'Kabul',
(
SELECT id_no FROM `country_types`
WHERE `type_id`='T' AND `description`='Independent State'
),
(
SELECT id_no FROM `country_types`
WHERE `type_id`='S' AND `description`=''
),
'1946/11/19', null, 'Afghan National Anthem', '', 'af.png', '', 'af_coa.png'
),
(
'AL', 'ALB', '008', 'Albania', 'Republic of Albania', 'Tirana',
(
SELECT id_no FROM `country_types`
WHERE `type_id`='T' AND `description`='Independent State'
),
(
SELECT id_no FROM `country_types`
WHERE `type_id`='S' AND `description`=''
),
'1955/12/14', null, 'Himni i Flamurit (United around the flag)', 'Ti, Shqipëri, më jep nder, më jep emrin Shqipëtar (You, Albania, give me honor, give me the name Albanian)', 'al.png', '', 'al_coa.png'
)
);
Can anyone see where I've gone wrong?
EDIT: I have a csv file with 198 countries that I have to insert into a mysql table. I can't insert the data as a csv because I need to convert 2 text fields ( type_id
and subtype_id
) into int
fields. Since I'm new to SQL, I searched this website and found the syntax to use. The single import works fine so I again searched this website to get the syntax for multiple inserts. This is the format I found:
INSERT INTO `countries` (col-names)
VALUES (
(data fields to insert),
(data fields to insert)
);
The above sample is 5 lines, might not show up this way, I'm still learning how to format my posts. If I can get 2 lines of values to work then I can add the other 196 lines.
I have placed a sample txt file here . Your browser might wrap so you might want to look at the source.
Try this syntax:
INSERT INTO countries
(
alpha2_id,
alpha3_id,
numeric_id,
common_name,
formal_name,
capital,
type_id,
subtype_id,
un_admission_date,
eu_accession_date,
anthem,
motto,
flag_name,
seal_name,
coa_name
)
VALUES (
(
'AF',
'AFG',
'004',
'Afghanistan',
'Islamic State of Afghanistan',
'Kabul',
(
SELECT id_no
FROM country_types
WHERE type_id='T'
AND description='Independent State'),
(
SELECT id_no
FROM country_types
WHERE type_id='S'
AND description=''),
'1946/11/19',
NULL,
'Afghan National Anthem',
'',
'af.png',
'',
'af_coa.png'
)
,
(
'AL', 'ALB', '008', 'Albania', 'Republic of Albania', 'Tirana',
(
SELECT id_no
FROM country_types
WHERE type_id='T'
AND description='Independent State'),
(
SELECT id_no
FROM country_types
WHERE type_id='S'
AND description=''),
'1955/12/14',
NULL,
'Himni i Flamurit (United around the flag)',
'Ti, Shqipëri, më jep nder, më jep emrin Shqipëtar (You, Albania, give me honor, give me the name Albanian)',
'al.png',
'',
'al_coa.png'
)
)
";
Your Query Should look like this
INSERT INTO countries
(
alpha2_id, alpha3_id, numeric_id,
common_name,formal_name, capital,
type_id, subtype_id, un_admission_date,
eu_accession_date, anthem, motto,
flag_name, seal_name, coa_name
)
Select 'AF', 'AFG', '004',
'Afghanistan', 'Islamic State of Afghanistan', 'Kabul',
(SELECT id_no FROM `country_types` WHERE `type_id`='T' AND `description`='Independent State')as TypeId,
(SELECT id_no FROM `country_types` WHERE `type_id`='S' AND `description`='')as subtype_id,
'1946/11/19' as un_admission_date, null,
'Afghan National Anthem', '', 'af.png','', 'af_coa.png'
/* In Column eu_accession_date You are Passing Null Please check whether
you column can contain null or not*/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.