SQL syntax error when trying Insert from select - mysql

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

Related

Mysql: copy selected columns to another table

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

How can I solve this MYSQL INSERT INTO query error?

I have a problem when I try to insert something in my MySQL database.
Here is the error:
Query 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 'LIMIT 1' at line 1 - Invalid query: INSERT INTO banlog (ID, ip, player, admin, reason, day, time) VALUES (NULL, 'Offline','4Gamers','4Gamers','3/3 warns', '3' , '2019-10-13 23:42:09') LIMIT 1
Here is the code:
$query = $this->db->query("INSERT INTO `banlog` (`ID`, `ip`, `player`, `admin`, `reason`, `day`, `time`) VALUES (NULL, 'Offline',?,?,'3/3 warns', '3' , '$time') LIMIT 1", array($info['name'], getUserData($this->session->userdata('logged_in')["id"], "name")));
Thanks.
Try removing LIMIT 1.
I hope this will solve your problem.

PHP SQL IF NOT EXISTS won't work

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.

SQL INSERT statement Error - Error at Where clause [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to add a where clause in a MySQL Insert statement?
I am writing a SQL INSERT STATEMENT with WHERE clause.
INSERT INTO House ( ID ,ADDRESS ) VALUES ('12','LONDON') WHERE OWNER='1';
Error i get :
ERROR 1064 (42000): 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 OWNER='1'' at line 1
WHERE is used in UPDATE query. Use UPDATE for your query if got where clause.
You can't use WHERE clause with INSERT Query..
If you want update the record already if the record exist
INSERT INTO House ( OWNER, ID ,ADDRESS ) VALUES ('1', '12','LONDON')
ON DUPLICATE KEY UPDATE ID = '12', ADDRESS = 'LONDON'
The 'ON DUPLICATE KEY' statement only works on PRIMARY KEY and UNIQUE columns
check this link for more info
INSERT INTO doesn't have a WHERE clause. Remove it.
You can onlu use the WHERE clause if you are selecting from a table, or updateing a table.
So you wouls typically have something like
INSERT INTO Table (Columns...) SELECT Columns... FROM SomeOtherTable WHERE Condition
In your case you need to use
INSERT INTO House ( ID ,ADDRESS ) VALUES ('12','LONDON')
WHERE clause doesnot work with INSERT INTO statement. You can use the clause with a SELECT statement
The correct code for you will be as:
INSERT INTO House(ID,ADDRESS) values(12,'LONDON');

MYSQL Insert on Duplicate

I'm trying to run the following statement:
INSERT INTO table (
as,
ad
,af,
ag,
ah,
aj
)
VALUES (
'a',
'b',
'c',
'd',
'e',
'f'
)
ON DUPLICATE KEY UPDATE (
aj='dv',
ah='ev',
ag='fv'
);
and getting 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 '(ag='dv',ah='ev',ah='fv')'
at line 3
Any advice?
thx
Skip the ()..
INSERT INTO site_domains_meta
(domainname,metatype,pagename,english,indonesian,japanese)
VALUES ('a','b','c','d','e','f')
ON DUPLICATE KEY UPDATE english='dv',indonesian='ev',japanese='fv';
http://dev.mysql.com/doc/refman/5.5/en/insert.html