I am trying to copy the data from online_class:class_category to categories:uuid,title
Table: online_class
...
category
...
...
category 1
...
...
category 2
...
...
...
...
Table: categories
uuid
title
...
...
...
...
INSERT INTO `categories` (`uuid`, `title`)
SELECT
(SELECT UUID()) AS `uuid`,
DISTINCT(`class_category`) AS `title`
FROM `online_class`
Getting this error
Error Code: 1064
You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax
to use near 'DISTINCT(`class_category`) as `title`
FROM `online_class`' at line 4
What's wrong in this query
I found another way to do this.
INSERT INTO `categories` (`uuid`, `title`)
SELECT UUID(), `class_category` FROM `online_class`
GROUP BY `class_category`
INSERT INTO categories(`uuid`, `title`)
SELECT uuid, DISTINCT(class_category)
FROM online_class
another example
INSERT INTO order_data(`item_name`, `item_id`)
SELECT item_name, item_id
FROM item_order
Related
Try to use this query
insert into `popups` (`GroupID`, `Name`,`ShortName`) VALUES
(SELECT `GroupID`,`Name`,`ShortName` FROM `temp` WHERE `GroupID` = '1')
#1064 - You have an error in your SQL syntax;
Tables popups and temp almost identical - hase same columns.
What is wrong in request?
It's either insert...values or insert...select in your case drop values
insert into `popups` (`GroupID`, `Name`,`ShortName`)
SELECT `GroupID`,`Name`,`ShortName` FROM `temp` WHERE `GroupID` = '1'
https://dev.mysql.com/doc/refman/8.0/en/insert.html
Well I only want to insert the data if it doesn't already exist in the table, if it exists nothing need to be done
insert into colleges (Id, Name, CreatedOn) values("20", "ASIET", "2017-12-14
06:44:32") where not exists (select id from colleges where name = "ASIET")
I'm getting error
Error Code: 1064. You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to use
near 'where not exists (select id from colleges where name = "ASIET")' at
line 1
And thanx in advance for the help
So if you don't want to have rows with the same id, and name then you can make both of them the primary key of table 'colleges' and then use insert ignore.
For making primary key use:
ALTER TABLE colleges
ADD PRIMARY KEY (id, name);
Then use insert ignore like this:
INSERT IGNORE INTO colleges (Id, Name, CreatedOn)
VALUES ("20", "ASIET", "2017-12-14 06:44:32");
I've got a question about SQL (php), I want to INSERT data in my table. But I want to use the IF NOT EXIST value.
What I've tried:
INSERT INTO vrienden (id, userid, vriendmetid, accepted) VALUES (null, '1', '20', '0') WHERE NOT EXISTS (SELECT * FROM vrienden WHERE userid='1' AND vriendmetid='20')
I'm not sure what's wrong, because I get the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE NOT EXISTS (SELECT * FROM vrienden WHERE userid='1' AND vriendmetid='20')' at line 1
Thanks.
You want insert . . . select, not insert . . . values:
INSERT INTO vrienden (id, userid, vriendmetid, accepted)
SELECT x.*
FROM (select null as id, '1' as userid, '20' as vriendmetid, '0' as accepted) x
WHERE NOT EXISTS (SELECT 1 FROM vrienden v WHERE v.userid = x.userid AND v.vriendmetid = x.vriendmetid);
However, you probably shouldn't be doing this in the INSERT. Instead, create a unique index/constraint:
create unique index unq_vrienden_userid_vriendmetid on vrienden(userid, vriendmetid);
This way, the database will ensure uniqueness of the columns, so your application does not have to.
This is my query :
SELECT vehicle,
CASE
WHEN vehicle IS NOT NULL
THEN (INSERT INTO tbl_vehicle_on_user (vehicle, userid) values
(SELECT `vehicle` FROM `tbl_missions` WHERE `id` = 4 ), (SELECT `id` FROM `tbl_users` WHERE `id` = 12))
FROM tbl_missions WHERE id = 4;
I need to insert a row to tbl_vehicle_on_user when vehicle is not null on id 4.
When i execute this query i receive this error from mysql workbench,
01:24:49 SELECT vehicle, CASE WHEN vehicle IS NOT NULL THEN (INSERT INTO tbl_vehicle_on_user (vehicle, userid) values (SELECT vehicle FROM tbl_missions WHERE id = 4 ), (SELECT id FROM tbl_users WHERE id = 12)) FROM tbl_missions WHERE id = 4 Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTO tbl_vehicle_on_user (vehicle, userid) values (SELECT vehicle FROM `tbl_mi' at line 4 0.000 sec
And i get a red line under 'INTO' when i hover over it, it says 'Syntax error, unexpected INTO, expecting ('.
I don't know what it means i tried to search the web but couldn't find anything if you know how to fix this i will appreciate it if you answer my question :)
THANKS!!
If you already know the userid should be 12, then just use 12 instead of SELECT id FROM tbl_users WHERE id = 12. Here is a valid insert-select statement.
INSERT INTO tbl_vehicle_on_user (vehicle, userid)
SELECT `vehicle`, 12 userid
FROM `tbl_missions`
WHERE `id` = 4 and vehicle is not null;
I'm trying to select a value (id) from a MySQL table and use it in a update statement - all in a MySQL query. The select is composed of 2 parts: if the id exists, it is returned; if not, 2 inserts are done and the id is returned.
I have the following query:
SELECT
(CASE a.id WHEN '' THEN (
DELIMITER //
INSERT INTO xxxx (item_id, date_created, date_modified) VALUES (3313, NOW(), NOW())//
INSERT INTO yyyy (item_id, locale_id, value, date_created, date_modified) VALUES(LAST_INSERT_ID(), 2, TRIM(SUBSTRING_INDEX('some text: 250 x 46 x 584', ':', 1)), NOW(), NOW())//
SELECT c.id FROM xxxx c JOIN yyyy d WHERE c.item_id=3313 AND d.value='some text' LIMIT 1
) ELSE a.id END
) AS trans_ref_id
FROM xxxx a JOIN yyyy b ON a.id = b.item_id
WHERE b.value='some text'
When i run it, i get the following error:
SQL Error (1064): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')
ELSE a.id
END
)
as trans_ref_id
FROM xxxx' at line 2
Am I having the wrong approach here? Where is this error coming from?
You cannot do this with SQL. You need to use cursors. The manual has an example for you to look at. Probably best to put this in a stored procedure.
Your creative attempt does not conform to the SELECT+INSERT syntax, which is:
INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name [(col_name,...)]
SELECT ...
[ ON DUPLICATE KEY UPDATE col_name=expr, ... ]