ERROR 1064: Cannot execute while loop in mysql using variables - mysql

I'd like to run the below query to insert into a table in a loop:
SET #maxid = ( select max( length( id ) ) from article ) ;
SET #x=1;
WHILE #x<= #maxid DO
INSERT INTO `article_responder`
(article_id, username, reply_id)
SELECT #x, p.username, p.id
FROM article AS a, post AS p
WHERE a.id=#x AND p.article_id=#x ;
SET #x = #x + 1;
END WHILE;
But I get this error:
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 'END WHILE' at line
Notice that article_id can not be auto incremented, because the bussiness loginc requires that after inserting the data, the next row in article_responder can be about any article. So I had no idea how to fill article_responder with usual joins.
How can I fix it?

I would suggest just making article_id a primary key auto increment column, and then doing the following INSERT INTO ... SELECT:
INSERT INTO article_responder (username, reply_id)
SELECT p.username, p.id
FROM post p
INNER JOIN article a
ON a.id = p.article_id;
Note that the logic WHERE a.id=#x AND p.article_id=#x is equivalent to saying that a.id = p.article_id.

Related

MARIADB : Insert and Update table based on data from another table

/* Here is the code i used to merge but i get error and am unable update
Error : SQL Error [1064][42000] You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the righr syntax to use near 'Merge temp2 as t' at line 1
*/
BEGIN
MERGE temp2 as t
using temp1 as s ON (t.slno = s.slno)
-- Insert values when data no present
WHEN NOT MATCHED THEN INSERT VALUES
(s.slno,s.name,s.address);
-- Update when values present
WHEN MATCHED then UPDATE SET
t.slno = s.slno,
t.name = s.name,
t.address = s.address;
END
You could probably use this:
INSERT INTO temp2 (slno, name, address) SELECT slno, name, address FROM temp1
ON DUPLICATE KEY UPDATE
slno = VALUES(slno), name = VALUES(name), address = VALUES(address)

Django migration sql for conditional triggers

I want to create a trigger that only inserts into table when the condition is met.
I've tried using various combinations of IF/BEGIN/END and WHERE, but Django returns me an SQL syntax error each time.
Here, type_user_id refers to the person who triggers the like, and user_id refers to the person who receives the notification. It should only trigger when they are not the same person.
operations = [
migrations.RunSQL(
"DROP TRIGGER like_notification",
"""
CREATE TRIGGER like_notification AFTER INSERT ON user_likes_article
FOR EACH ROW
IF (select user_id from channel WHERE channel_id IN
(SELECT channel_id FROM article WHERE article_id = NEW.article_id)) <> NEW.user_id
BEGIN
INSERT INTO notification (type, type_id, article_id, is_read, type_user_id, user_id)
VALUES ('like', NEW.article_id, NEW.article_id, false, NEW.user_id,
(SELECT user_id from channel WHERE channel_id IN
(SELECT channel_id FROM article WHERE article_id = NEW.article_id)
)
)
END;
"""
)
]
>>>django.db.utils.ProgrammingError: (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 'BEGIN\n INSERT INTO notification (type, type_id, article_id, is_re' at line 5")
and
CREATE TRIGGER like_notification AFTER INSERT ON user_likes_article
FOR EACH ROW
BEGIN
DECLARE #user_same Boolean;
SELECT 1 into #user_same
FROM channel
WHERE channel_id IN (select channel_id FROM article where article_id = NEW.article_id)
AND user_id = NEW.user_id;
IF #user_same <> 1
THEN
INSERT INTO notification (type, type_id, article_id, is_read, type_user_id, user_id,)
VALUES ('like', NEW.article_id, NEW.article_id, false, NEW.user_id, (
SELECT user_id from channel WHERE channel_id IN (
SELECT channel_id FROM article WHERE article_id = NEW.article_id)
)
END IF;
END;
>>>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 '#user_same Boolean;\n SELECT 1 into #user_same\n FRO' at line 4"
and
CREATE TRIGGER like_notification AFTER INSERT ON user_likes_article
FOR EACH ROW
INSERT INTO notification (type, type_id, article_id, is_read, type_user_iduser_id)
VALUES ('like', NEW.article_id, NEW.article_id, false, NEW.user_id,
(
SELECT user_id from channel WHERE channel_id IN
(SELECT channel_id FROM article WHERE article_id = NEW.article_id)
)
WHERE NOT user_id = NEW.user_id;
>>>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 'WHERE NOT user
I don't really understand why none of them work. Any help would be much appreciated!
#variables are not DECLARED.
Either:
DECLARE user_same BOOLEAN;
SELECT 1 INTO user_same WHERE ...
or
SELECT #user_same := 1 WHERE ...
Better yet, this avoids the need for the variable.
IF (EXISTS SELECT * FROM ...)
Also, don't use the construct IN ( SELECT ... ); it is usually better to use WHERE EXISTS ( SELECT * FROM ... ) or JOIN ... ON ....
(There may be more problems after fixing those.)
Another easy way to do this.
You should add any symbols between ; delimiters for the appropriate converting MySQL syntax to the raw SQL.
CREATE TRIGGER trigger_name BEFORE INSERT ON table
FOR EACH ROW
BEGIN
IF NEW.number <> 'anynumber' AND NEW.number <> 'anynumber'
THEN
SET NEW.number = 'anynumber'; --
END IF; --
END
This example works due to the dash symbols.

select if and update issue

Am using this sql to do an update on duplicate key
IF (SELECT COUNT(*) FROM `mlm_settings` WHERE `key` = 'notify_type' AND `user_id`=7 >0 )
UPDATE mlm_settings SET value='2' WHERE user_id = 7
ELSE
BEGIN
INSERT INTO `mlm_settings` (`key`, `value`,`user_id`) VALUES ('notify_type', '2',7)
END
by i get an sql error in mysql 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 'IF (SELECT COUNT(*) FROM
`mlm_settings` WHERE `key` = 'notify_type' AND `user_id' at line 1
i can't figure what is the cause of the error, as the names of the tables are valid and the values are of the same data type
what could be the error?
You can only use IF statements in stored procedures, not normal queries.
If there's a unique index on (key, user_id) in the table, you can use:
INSERT INTO mlm_settings (`key`, value, user_id) VALUES ('notify_type', '2', 7)
ON DUPLICATE KEY UPDATE value = '2';
IF control block cannot be used OUTSIDE of functions. Try this:-
SELECT IF( EXISTS(
SELECT COUNT(*) FROM `mlm_settings` WHERE `key` = 'notify_type' AND `user_id`=7 >0), 1, 0)

MySQL Trigger Syntax Error union two/three tables

I wrote following code:
create trigger money after update on `things`
for each row
begin
Select #c1=sum(`thing_cost`) from `things`
UNION
Select #c2=sum(`salary`) from `dude_base`
Update `current` set `curr_cash`=#c1*#c2/100
end;
$$
Table "things" has got:
id1 (PK)
name
thing_cost
Table dude_base has got:
id2 (PK)
salary
name, etc. irrevelant
Table current has got:
id1 (FK)
id2(FK)
curr_cash
I got 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 'Update `current` set `curr_cash`=#c1*#c2/100; END' at line 7
Any help?
I think you should have a semicolon ; like
Select #c2=sum(`salary`) from `dude_base`;
to end this query because UPDATE is another query
// edit
try it like this
SET #c1 = (SELECT sum(`thing_cost`) from `things`);
SET #c2 = (SELECT sum(`salary`) from `dude_base`);
UPDATE `current` SET `curr_cash` = #c1 * #c2 / 100

MySQL Error querying from PHP: IF EXISTS

I have this PHP Code with Query to mysql database:
$query2 = "IF( EXISTS (SELECT * FROM shipcargo WHERE shipid='$shipid' AND item='$item' AND price='$price'))
BEGIN
UPDATE shipcargo SET amount = amount+'$amount' WHERE shipid='$shipid' AND item='$item' AND price='$price'
END
ELSE
BEGIN
INSERT INTO shipcargo (shipid, item, amount, price) VALUES('$shipid', '$item', '$amount', '$price')
END";
mysql_query($query2) or die(mysql_error());
The Error Returned 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 'IF( EXISTS (SELECT * FROM shipcargo WHERE shipid='11' AND item='WheatBastard' AN' at line 1
syntax is wrong! Take a look at http://dev.mysql.com/doc/refman/5.0/en/exists-and-not-exists-subqueries.html
I believe it's
IF EXISTS (SELECT * FROM
Rather than
IF (EXISTS ( SELECT
You don't need a bracket with the IF statement