select inside insert with more data to be inserted - mysql

I'm facing a problem with select inside insert statements.
I've take a look at the questions similar to this but still the query is not working.
first, I'm working with MySQL version 5.6.24 with engine InnoDB, and I'm trying to insert this row:
INSERT INTO form (SELECT course_name FROM course WHERE course_id ='1' ), '123456','5','3','6','1','3','6','1','2','5','6','1','4','1','2','3','good','not bad','bad')
I want the first column to be retrieved (which is only one value), but not the other.
I've tried many syntax formats, with VALUES, with semicolon, with more parentheses, etc... but non work. Here is the 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 ' '123456','5','3','6','1','3','6','1','2','5','6','1','4','1','2','3','good','no' at line 1
Thanks.

Use an INSERT-SELECT:
INSERT INTO form
SELECT course_name, '123456','5','3','6','1','3','6','1','2','5','6','1','4','1','2','3','good','not bad','bad'
FROM course
WHERE course_id = '1'

Related

Insert data partially into a table

I am trying to insert into my table something with partial data and leaving the rest default but somehow it keeps saying wrong syntax
My query is like this:
INSERT INTO day
(1030, 1100, date, tech)
VALUES ('356-635-3633', '356-635-3633', '2019-04-07', 'Thy')
#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 '1030, 1100, date, tech) VALUES('356-635-3633', '356-635-3633', '2019-04-07', 'Th' at line 1
A query like this works though:
INSERT INTO day (date, tech) VALUES ('2019-04-07', 'Thy')
The datatype for all those columns are varchar(30)
You would need to quotes these all-digits identifiers, using backticks;
INSERT INTO day
(`1030`, `1100`, date, tech)
VALUES ('356-635-3633', '356-635-3633', '2019-04-07', 'Thy')
From the documentation:
Identifiers may begin with a digit but unless quoted may not consist solely of digits.
Demo on DB Fiddle (you can uncomment the original column list to generate the error).
NB: day and date correspond to names of MySQL functions. Using them like you do do not generate error, but it would still be a good idea to surround them with backticks a well, just to avoid any ambiguity, hence:
INSERT INTO `day`
(`1030`, `1100`, `date`, tech)
VALUES ('356-635-3633', '356-635-3633', '2019-04-07', 'Thy')

MySQL INSERT SELECT not Inserting

I am trying tor un an insert select that looks like this:
INSERT INTO users_extension_usage (userid, extensionid, complete)
SELECT '3', extensions.id FROM extensions WHERE extensions.folder='definitions', '1'
however it has a problem when I run it in phpMyAdmin that refers to the '1':
#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 ' '1'' at line 2
I have tried many different combinations of SELECT, INSERT etc but I cat see the problem. I have looked at any different posts on here and other forums too but alas most suggest the syntax I am using!
I have tried to set up an SQL Fiddle here: http://sqlfiddle.com/#!9/9f460
Any suggestions for how this might work will be gratefully accepted :)
You are missing the third column:
INSERT INTO users_extension_usage(userid, extensionid, complete)
SELECT '3', extensions.id, '1' as complete
FROM extensions
WHERE extensions.folder = 'definitions';
The '1' belongs in the SELECT statement, not after the WHERE.

mysql insert from select query returning syntax error [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
So, I run an online imageboard and I am updating to a newer imageboard software. Rather than manually enter the board information for each new imageboard, I wanted to just port over the fields I need from the old table to the new one, but PHPMYADMIN is giving me a mysql syntax error and I don't know what the problem is:
INSERT INTO `tryboards` (uri, title)
SELECT name, desc FROM `aasboards`;
This should move the data from the old table to the new, yes? The aasboards table has several columns I want to omit. The tryboards table contains 3 fields, the 3rd one being subtitle, which is nullable and shouldn't be needed for this query.
EDIT: the error is as follows:
Error
SQL query: Documentation
INSERT INTO tryboards ( uri, title )
SELECT name, DESC
FROM aasboards
MySQL said: Documentation
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 'desc FROM aasboards' at line 2
desc is a reserved word:
INSERT INTO `tryboards` (uri, title)
SELECT name, `desc` FROM `aasboards`;

Multiple Queries in Triggers

I'm fairly new to using triggers and have a tiny question.
I have a trigger finds a match between a newly inserted enquiry and a customer table.
INSERT INTO customersmatched (customerID,enquiryID) SELECT id, NEW.id FROM customer AS c WHERE c.customerName=NEW.companyName HAVING COUNT(id)=1;
I then need to update the newly inserted enquiry so it has a status which shows it's matched (but only if it has matched). So I tried adding this line after the insert.
UPDATE enquiry SET status="Live-Enquiry" WHERE id IN ( SELECT enquiryID FROM customersmatched WHERE enquiryID = NEW.id);
Except I get this error:
MySQL said: #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 'UPDATE enquiry SET status="Live-Enquiry" WHERE id
IN ( SELECT enquiryID FROM cus' at line 5
How do I allow multiple queries within a trigger. I've tried doing something like in this link: Multiple insert/update statements inside trigger?
But doesn't work either. I'm using phpmyadmin btw. Can anyone help? :D
If you have ansi quotes enabled then you can't use double quotes as a string literal, and need to use single quotes instead. see: http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_ansi_quotes Otherwise, I don't see any syntax errors that jump out at me.
Try changing SET status="Live-Enquiry" to SET status='Live-Enquiry'
EDIT:
What is the purpose of the first query? I'm not sure you need the HAVING in that query. If want a distinct list of matches, just use DISTINCT
INSERT INTO customersmatched (customerID,enquiryID)
SELECT DISTINCT id, NEW.id
FROM customer AS c
WHERE c.customerName=NEW.companyName;
The second query, if I understand it correctly, can be simplified to this:
UPDATE enquiry
SET status='Live-Enquiry'
WHERE id = NEW.id;

INSERT values from one table to another using SQL Query

I am trying to run this SQL Query:
INSERT INTO controldata (field,value)
VALUES (customer_billing_product_type,
SELECT name from customer_billing_product_types)
to insert all rows in the customer_billing_product_types table into the controldata table with the field always being customer_billing_product_type but i get an SQL Error saying:
#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
'SELECT name from customer_billing_product_types)' at line 1
The INSERT as you wrote it expects to insert one record, but then in values an entire set of records is returned. Try to change your query to
INSERT INTO controldata(field, value)
SELECT customer_billing_product_type, t.name FROM customer_billing_product_types t;
This selects all t.name values from customer_billing_product_types, adds customer_billing_product_type as column and inserts all the results into controldata.
You are using wrong syntax try below:
INSERT into controldata (field1) SELECT name from customer_billing_product_types;
If you want to keep a field as constant then you can use as per below:
INSERT into controldata (field,value) SELECT 'customer_billing_product_types',name from customer_billing_product_types;