MariaDB - complex trigger query after update - mysql

I have three tables ( restaurants - votes - comments) . I want to create a trigger which let me update two fields in restaurants ( restaurant_rate_average , restaurant_comment_count ) . Here is my trigger :
DELIMITER $$
CREATE
TRIGGER `change comment status` AFTER UPDATE ON `comments`
FOR EACH ROW
BEGIN
UPDATE restaurants
INNER JOIN votes ON restaurants.restaurant_id = votes.vote_foreign_key
SET restaurants.restaurant_comment_count = SELECT (COUNT(votes.vote_id))
WHERE votes.vote_model = 'restaurant'
AND WHERE old.comment_status<>NEW.comment_status;
UPDATE restaurants
INNER JOIN votes ON restaurants.restaurant_id = votes.vote_foreign_key
SET restaurants.restaurant_rate_average = SELECT (AVG(votes.vote_num))
WHERE votes.vote_model = 'restaurant'
AND WHERE old.comment_status<>NEW.comment_status;
END;
$$
DELIMITER ;
Actually I have an 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 'SELECT (COUNT(votes.vote_id))
WHERE votes.vote_model = 'restaurant'
' at line 8
I've tried to make complexion less and replace SELECT (COUNT(votes.vote_id)) with 2 and I get new 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 'WHERE comments.comment_status<>NEW.comment_status;
UPDATE resta' at line 9
How to resolve it?
EDITED :
here is my pseudo tables :
restaurants:
restaurant_id, restaurant_comment_count(int) , restaurant_rate_average(int)
comments:
comment_id , comment_status(boolean) , comment_content(varchar) , votes_vote_id(id)
votes:
vote_id , vote_foreign_key(id), vote_model(varchar)
EDITED 2 :
I create new simple trigger which just join three tables and it works like charm:
BEGIN
UPDATE restaurants res
INNER JOIN votes v ON v.vote_foreign_key = res.restaurant_id AND v.vote_foreign_key = 'restaurant'
INNER JOIN comments cm ON cm.votes_vote_id = v.vote_id
SET res.restaurant_rate_average = 2
WHERE cm.comment_status = NEW.comment_status;
END
But if I replace SET res.restaurant_rate_average = 2 with SELECT AVG(v.vote_id) I got an error. I also edit last trigger to :
BEGIN
DECLARE RESTAURANT_RATE_AVERAGE INTEGER;
SET RESTAURANT_RATE_AVERAGE := (SELECT AVG(votes.vote_num)
UPDATE restaurants res
INNER JOIN votes v ON v.vote_foreign_key = res.restaurant_id AND v.vote_foreign_key = 'restaurant'
INNER JOIN comments cm ON cm.votes_vote_id = v.vote_id
SET restaurant_rate_average = RESTAURANT_RATE_AVERAGE
WHERE cm.comment_status = NEW.comment_status;
END
I got an error in line 4. All I want is replace static number with SELECT AVG(votes.vote_num)

DROP TRIGGER IF EXISTS `update_comment_status`;CREATE DEFINER=`root`#`localhost` TRIGGER `update_comment_status` AFTER UPDATE ON `comments` FOR EACH ROW BEGIN
DECLARE RESTAURANT_RATE_AVERAGE INTEGER;
DECLARE RESTAURANT_COMMENT_COUNT INTEGER;
SET RESTAURANT_RATE_AVERAGE := (SELECT AVG(votes.vote_num) FROM votes);
UPDATE restaurants res
INNER JOIN votes v ON v.vote_foreign_key = res.restaurant_id AND v.vote_foreign_key = 'restaurant'
INNER JOIN comments cm ON cm.votes_vote_id = v.vote_id
SET restaurant_rate_average = RESTAURANT_RATE_AVERAGE
WHERE cm.comment_status = NEW.comment_status;
SET RESTAURANT_COMMENT_COUNT := (SELECT COUNT(votes.vote_id) FROM votes);
UPDATE restaurants res
INNER JOIN votes v ON v.vote_foreign_key = res.restaurant_id AND v.vote_foreign_key = 'restaurant'
INNER JOIN comments cm ON cm.votes_vote_id = v.vote_id
SET restaurant_comment_count = RESTAURANT_COMMENT_COUNT
WHERE cm.comment_status = NEW.comment_status;
END

Related

Comparing primary Id of the same row in same table

Can't find the same questions as mine I'm hoping you guys can help me.
I have this query for my SP what I need to do is check if the company Id is the same based on the parameter given by the user.
I have two parameters which are paramOrgA and paramOrgB which will be supplied by the user.
What I did was repeat the same query and set the returned value in two variables which is
orgAId and orgBId then use the IF statement to compare the value.
is there another way to accomplish this or should I say optimized way to do this?
Below is the query I wrote.
BEGIN
declare orgAId int;
declare orgBId int;
declare orgStatus bool;
SET orgAId = (SELECT c.Id
FROM Members as `m`
INNER JOIN Company as `c`
ON m.CompanyId = c.Id
WHERE m.Id = paramOrgA);
SET orgBId = (SELECT c.Id
FROM Members as `m`
INNER JOIN Company as `c`
ON m.CompanyId = c.Id
WHERE m.Id = paramOrgB);
IF (orgAId = orgBId) THEN
set orgStatus = true;
ELSE
set orgStatus = false;
END IF;
select orgStatus as 'CompanyStatus';
END IF;
END
One query will do
SELECT count(distinct c.Id) > 1 as 'CompanyStatus'
FROM Members as `m`
INNER JOIN Company as `c` ON m.CompanyId = c.Id
WHERE m.Id IN (paramOrgA, paramOrgB)
Even fewer keystrokes:
SELECT COUNT(DISTINCT CompanyId) > 1 as 'CompanyStatus'
FROM Members
WHERE Id IN (paramOrgA, paramOrgB)

How can I update a column value of a table in DB1 with a column value from a table in DB2?

I'm trying to perform a simple update in SQL between 2 tables from different DB's. The challenge is that in order for the value to be updated it must meet certain conditions. I have used the join statements to meet the conditions and when I go to test the value from table B it is not being updated into table A. Here is what I've done so far.
USE [dbo]
GO
CREATE PROCEDURE
(
#User_ID = INT,
#Batch_ID VARCHAR(32)
)
DECLARE #locid int
SELECT #locid
FROM OtherDB.dbo.User AS UL
WHERE UL.User_ID = #User_Id
and User_Type = 1;
UPDATE M
SET
M.Number = W.Number
FROM dbo.tableA AS W
JOIN dbo.tableB AS B ON B.ID = W.ID
JOIN dbo.tableC AS C ON C.ToolA = B.ToolA
JOIN dbo.tableD as D ON D.Zone = W.Zone_Name
JOIN OtherDB.dbo.tableMax AS M ON M.LID = #locid
AND M.Tool = C.Other_Tool
AND M.Zone = D._Other_Zone
AND M.Station = W.Station
WHERE W.User_ID = #User_ID
AND W.Batch_ID = #Batch_ID
SET NOCOUNT OFF;
The update statement is updating table M in otherDB, not table A on the current database.
You might revise the stored procedure to be
Update A
Set A.Number = W.Number
From dbo.tableA A
....

Using in paramenter in subquery?

Is posible to using in paramenter in subquery?
I'm trying to join three table, and using paramenter in subquery, but I'm getting error:
drop procedure if exists displayFilmInfo;
delimiter //
create procedure displayFilmInfo(in in_category_id tinyint, in in_language_id tinyint)
begin
if in_category_id != 0 and in_language_id != 0 then
select film.title, after_category.name from film
inner join film_category on film_category.film_id = film.film_id
inner join (
select category_id, name from category when category.category_id = in_category_id
) after_category on after_category.category_id = film_category.category_id
inner join (
select language_id, name from language when in_language_id = language.language_id
) after_language on after_language.language_id = film.language_id
end if;
end //
delimiter ;
call displayFilmInfo();
It show:
Query OK, 0 rows affected, 1 warning (0.00 sec)
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 'when category.category_id = in_category_id
) after_category on afte' at line 8 ERROR 1305 (42000): PROCEDURE sakila.displayFilmInfo does not exist
Please update your select statement to -
select film.title,
after_category.name
from film
inner join film_category on film_category.film_id = film.film_id
inner join (select category_id,
name
from category
where category.category_id = in_category_id) after_category on after_category.category_id = film_category.category_id
inner join (select language_id,
name
from language
where in_language_id = language.language_id) after_language on after_language.language_id = film.language_id

What am i doing wrong with this update from select

I have been triying to make an update from a select the console keeps showing an error message
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 'from
(
Select
a.id_activity as stat
, a.date_reg as l_clock
' at line 5
I would appreciate some help with it.
update user_stat set
clock_stat = a.stat
, datetime_lclock = a.l_clock
, date_fclock = a.f_clock
from
(
Select
a.id_activity as stat
, a.date_reg as l_clock
, date(c.date_reg) as f_clock
from log_activity a
inner join
(
select
max(id_reg) as last_Act,
min(id_reg) as first_Act
from log_activity
where uid = 1
)b on a.id_reg = b.last_Act
left join log_activity c on c.id_reg = b.first_Act
)a
where uid = 1;
You need to set value later, so the general syntax is some like :
UPDATE TABLEA a
JOIN TABLEB b ON a.join_colA = b.join_colB
SET a.columnToUpdate = [something]
update user_stat
join
(
Select
a.id_activity as stat
, a.date_reg as l_clock
, date(c.date_reg) as f_clock
from log_activity
) a
inner join
( select
max(id_reg) as last_Act,
min(id_reg) as first_Act
from log_activity where uid = 1
)b on a.id_reg = b.last_Act
left join log_activity c on c.id_reg = b.first_Act
set
clock_stat = a.stat
, datetime_lclock = a.l_clock
, date_fclock = a.f_clock

how to solve MYSQL ERROR 1248 (42000): Every derived table must have its own alias [duplicate]

I need to run this query :
UPDATE (
SELECT r.*
FROM booked r
INNER JOIN (
SELECT a.st_code as from_t
, b.st_code as to_t
FROM `stops_at` a
CROSS JOIN `stops_at` b
WHERE (a.stop_no < b.stop_no)
and (a.train_no = b.train_no)
and (a.train_no = '11280')
) new
ON (r.st_from = new.from_t)
and (r.st_to = new.to_t)
and r.date = '2013-04-16'
) temp
SET temp.seat_ac = temp.seat_ac-5
but on execution it gives an error:
#1288-The target table temp of the UPDATE is not updatable.
Any solutions?
I think your UPDATE syntax is incorrect. See if this works:
UPDATE booked r
INNER JOIN (
SELECT a.st_code as from_t
, b.st_code as to_t
FROM `stops_at` a
CROSS JOIN `stops_at` b
WHERE (a.stop_no < b.stop_no)
and (a.train_no = b.train_no)
and (a.train_no = '11280')
) new
ON r.st_from = new.from_t
and r.st_to = new.to_t
and r.date = '2013-04-16'
SET r.seat_ac = r.seat_ac-5