I've recently started working on MySQL n right now i want to create a trigger but MySQL is returning an error in my syntax.
delimiter $$;
create trigger abc after insert on ratings
for each row
begin
set #n1 = select avg(rating) from ratings join users where ratings.uname=users.uname
and ratings.bookid=new.bookid users.`type`='admin' or users.`type`='critic';
update books set avgcriticrating = #n1 where bookid=new.bookid;
end;
The select statement runs perfectly when fired on its own but gives an error when i use it inside of the trigger.
Here's the error that MySQL gives
#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 avg(rating) from ratings join users where ratings.uname=users.uname an' at line 4
Both the books and ratings table contain a field called bookid.
please help
If you want a single value from a select statement, the statement has to be in (brackets).
set #n1 = (select avg(rating) from ratings join users where ratings.uname=users.uname
and ratings.bookid=new.bookid users.`type`='admin' or users.`type`='critic');
The next error will occur at new.bookid users - there might be an and or an or missing.
Related
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
For MySQL, If I stored the userid and a token in the user table and only allows to insert record in another table if the supplied userid and token matches.I've tried
INSERT INTO product(description)
VALUES('123')
WHERE EXISTS
(SELECT 1 FROM users where userid='myuserid' AND token='ABCD')
The following SQL statement produces error
"check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE EXISTS (SELECT 1 FROM users where userid='29')'"
However for update I can update successfully with
UPDATE product
SET description='123'
WHERE EXISTS
(SELECT 1 FROM users where userid='myuserid' AND token='ABCD')
Can any experts please help to advise.
I need the most efficient way to verify the user token is correct before doing the insert.
Please try using a trigger instead:
CREATE TRIGGER tokenValidator
BEFORE INSERT
ON product
FOR EACH ROW BEGIN
IF (SELECT 1 FROM users where userid='myuserid' AND token='ABCD') THEN
INSERT INTO product(description) VALUES('123')
END IF
Error message:
SQL (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 'END' at line 15
DELIMITER //
CREATE TRIGGER fill_address AFTER UPDATE ON orders
FOR EACH ROW
BEGIN
UPDATE orders
SET address = CONCAT_WS(
' ',
payment_method,
payment_address_1,
payment_address_2,
payment_city,
payment_postcode,
payment_country,
payment_zone)
WHERE id IN (SELECT id FROM INSERTED)
END //
DELIMITER
When you only run a single statement you don't need to do anything special. You send the code to the server and you're done:
SELECT COUNT(*) AS customer_count FROM customer
However, if you want to send more than one query at a time you need to tell MySQL where each statement ends. Otherwise, the SQL parser will treat everything as one query and will abort parsing wherever the query stops making sense:
SELECT COUNT(*) AS customer_count FROM customer
SELECT COUNT(*) AS order_count FROM order
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 COUNT(*) AS order_count FROM order' at line 2
In almost all MySQL clients out there, default delimiter is semicolon:
SELECT COUNT(*) AS customer_count FROM customer;
SELECT COUNT(*) AS order_count FROM order;
Triggers and other pieces of user defined routines are a special case because you need to distinguish between two situations:
Element creation
Element execution
When you create the trigger, everything you send is a single statement: all the CREATE TRIGGER code. When the trigger executes, each statement inside the trigger body is an independent query. How can we distinguish both situations with the same code? The trick is to use the standard ; separator in the trigger body (where we don't have control about it) but temporarily change the delimiter when executing the CREATE TRIGGER code (where we can use the DELIMITER command offered by most clients).
The first version of your code was missing all separators. Current version is missing trigger body separators.
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)
I get an error (1064) when attempting to run the following... (MySql 5.5.9)
query:
CREATE TRIGGER clearChat AFTER INSERT ON chat
FOR EACH ROW
BEGIN
DELETE p.* FROM chat p LEFT JOIN (SELECT t.id FROM chat t ORDER BY t.id DESC LIMIT 50) x ON x.id = p.id WHERE x.id IS NULL
END;
the error is:
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 'END' at line 5
Any assistance would be great.
Last Edit: Updated to show the 'FOR EACH ROW' and 'BEGIN'
You're missing FOR EACH ROW before DELETE: http://dev.mysql.com/doc/refman/5.1/en/create-trigger.html
Edit: There are more issues. The correct syntax is below:
delimiter |
CREATE TRIGGER clearChat AFTER INSERT ON chat
FOR EACH ROW BEGIN
DELETE p.* FROM chat p LEFT JOIN (SELECT t.id FROM chat t ORDER BY t.id DESC LIMIT 50) x ON x.id = p.id WHERE x.id IS NULL;
END;
|
delimiter ;
Edit 2:
I don't think that query is allowed to be in a trigger at all based on this http://dev.mysql.com/doc/refman/5.1/en/faqs-triggers.html#qandaitem-B-5-1-9:
A trigger can access both old and new
data in its own table. A trigger can
also affect other tables, but it is
not permitted to modify a table that
is already being used (for reading or
writing) by the statement that invoked
the function or trigger.
Since you aren't using OLD or NEW, I don't think you can modify chat since the trigger is triggered on inserts to chat.
I had the same problem with the following statement, it ALWAYS gave me a syntax error on even this simplified delete statement (originally was DELETE FROM APP_CACHE_VIEW WHERE APP_UID = OLD.APP_UID;):
CREATE TRIGGER APPLICATION_DELETE BEFORE DELETE ON APPLICATION
FOR EACH ROW
BEGIN
DELETE FROM APP_CACHE_VIEW;
END
If I changed the SQL command to the following then it WORKED but I don't understand why:
DELIMITER $$
CREATE TRIGGER APPLICATION_DELETE BEFORE DELETE ON APPLICATION
FOR EACH ROW
BEGIN
DELETE FROM APP_CACHE_VIEW;
END$$
DELIMITER ;