Mysql Subquery Outer Where - mysql

Im trying to write a mysql query that will find the value to insert via a sub query and only insert into the row that has the id that is specified mysql is giving me this 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 'WHERE product_id = '1'' at line 8
But i don now know what the synax should be any help is appreciated bellow is my current query.
Lewis
INSERT INTO oc_product
(tax_class_id)
(
SELECT tax_class_id
FROM oc_tax_class
WHERE title = 'Taxable Goods'
)
WHERE product_id = '1'
EDIT: im a moron and i should have been using UPDATE not insert its fixed now.

If I'm understanding your question correctly, I think you or looking to insert a new row with product id = 1:
INSERT INTO oc_product (product_id, tax_class_id)
SELECT 1, tax_class_id
FROM oc_tax_class
WHERE title = 'Taxable Goods'

Related

How to create trigger for updating in mysql

How to Update table1 if the table1 primary key existing in table2. I am using following code but it gives mysql syntax error.
CREATE TRIGGER upd_selectoin
BEFORE UPDATE ON customer
FOR EACH ROW
BEGIN
IF NEW.customer_sk IN(SELECT quotation_cname FROM quotation) THEN
UPDATE customer s JOIN quotation m
ON m.quotation_cname = s.customer_sk
SET s.grade = 2
WHERE s.customer_sk = NEW.customer_sk;
END IF;
I got the following Error
#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 '' at line 9
I want to update the customer table grade column if the customer_sk existing in quotation table.Please help me

mysql insert clause gives #1064 error

I have a mysql table school
id dob name surname
1 03.04.2011 jj
2 14.07.1999 na
.. ............ ..
I have many rows of data in the above table. Now what I want to do is; fill the surname column using an insert clause as follows
INSERT INTO `school` (surname) VALUES
('highvision'),
('oceanof'),
('malindimetho'),
('tahdhibprima'),
('stpatricks'),
...............
('stpatricks');
note: the number of rows I am inserting equals the number of rows in my table
On using the above INSERT statement I get the following 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 '' at line ..
How can I insert the rows?
You cannot use INSERT to fill the table.
You can use the following:
UPDATE `school` SET `surname` = 'highvision' WHERE `id` = '1'

select inside insert with more data to be inserted

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'

How to crosscheck two tables and insert relevant data into a new table in MYSQL?

I'm trying to cross-check a row that exists in two tables using a MySQL query in phpmyadmin and then, if a userID is found in both tables, insert their userID and user name into another table. Here's my code:
INSERT INTO userswithoutmeetings
SELECT user.userID
IF('user.userID'='meeting.userID');
I keep getting plagued by this 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
'IF('user.userID'='meeting.userID')' at line 3
Other statements I've tried have worked but not deposited the values in the table.
Something like
INSERT INTO userswithoutmeetings(userId,userName)
SELECT DISTINCT a.userId, a.userName
FROM table1 a
INNER JOIN table2 b ON (a.userId = b.userId)

MySQL foreign key problem

I'm getting that error running on MySQL 5.5.8
Mysql2::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
(SELECT id FROM products WHERE id = NEW.brand_id) IS NULL;
EN' at line 6:
CREATE TRIGGER fk_brands_products_insert
BEFORE INSERT ON brands
FOR EACH ROW BEGIN
SELECT
RAISE(ABORT, "constraint violation: fk_brands_products")
WHERE
(SELECT id FROM products WHERE id = NEW.brand_id) IS NULL;
END;
What could be wrong?
I suspect the problem is there is no FROM clause in your select statement.
Are you sure you can raise errors inside a query like that? I can't find it anywhere. I think the proper way would be to select a COUNT or EXISTS and return the result of that INTO a variable. Then, after the query, raise an error if the result doesn't meet your expectations.
Something like this:
SELECT count(id) INTO IDCOUNT FROM products WHERE id = NEW.brand_id;
Wouldn't it be better by the way to just add a real constraint? Or do you use a storage type that doesn't support that?