Problem with syntax when try add trigger in mysql - mysql

I need help with my SYNTAX error
I try to add trigger into mysql by still have syntax error.
I will be glad for any help
When I try add this trigger i still have error 1064
Full error response is: Error Code: 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 19 MySQL version is 5.7
CREATE TRIGGER delete_przejazdy_logger AFTER DELETE ON przejazdy FOR EACH ROW
BEGIN
INSERT INTO zmiany (rodzaj, data_modyfikaji, uzytkownik,
id_pasazera, imie_nazwisko, tel_1, tel_2, tel_3, email,
id_odbiorcy, imie_nazwisko2, tel2_1, tel2_2, dokument2,
id_adres_z, kraj_z, miejscowosc_z, adres_z, kod_z, tel_stac_z,
id_adres_do, kraj_do, miejscowosc_do, adres_do, kod_do, tel_stac_do,
data, osob, fotelik, detale, ilosc_bagazu)
(SELECT 1, CURRENT_DATE, SUBSTRING_INDEX(USER(), '#', 1),
pasazerowie.id AS id_pasazera, pasazerowie.imie_nazwisko, pasazerowie.tel_1, pasazerowie.tel_2, pasazerowie.tel_3, pasazerowie.email,
odbiorcy.id AS id_odbiorcy, odbiorcy.imie_nazwisko AS imie_nazwisko2, odbiorcy.tel_1 AS tel2_1, odbiorcy.tel_2 AS tel2_2, odbiorcy.dokument AS dokument2,
adresy_z.id AS id_adres_z, adresy_z.kraj AS kraj_z, adresy_z.miejscowosc AS miejscowosc_z, adresy_z.adres AS adres_z, adresy_z.kod AS kod_z, adresy_z.tel_stac AS tel_stac_z,
adresy_do.id AS id_adres_do, adresy_do.kraj AS kraj_do, adresy_do.miejscowosc AS miejscowosc_do, adresy_do.adres AS adres_do, adresy_do.kod AS kod_do, adresy_do.tel_stac AS tel_stac_do,
OLD.data, OLD.osob, OLD.fotelik, OLD.detale, OLD.ilosc_bagazu
FROM pasazerowie
LEFT JOIN pasazerowie odbiorcy ON OLD.id_odbiorcy = odbiorcy.id
JOIN adresy adresy_do ON OLD.id_adres_do = adresy_do.id
JOIN adresy adresy_z ON OLD.id_adres_z = adresy_z.id
WHERE pasazerowie.id = OLD.id_pasazera);
END;
I add triggers on this same db for logs on creating records and works fine.
Here is the code which works good:
CREATE TRIGGER add_przejazdy_logger AFTER INSERT ON przejazdy FOR EACH ROW
BEGIN
INSERT INTO zmiany (rodzaj, data_modyfikaji, uzytkownik,
id_pasazera, imie_nazwisko, tel_1, tel_2, tel_3, email,
id_odbiorcy, imie_nazwisko2, tel2_1, tel2_2, dokument2,
id_adres_z, kraj_z, miejscowosc_z, adres_z, kod_z, tel_stac_z,
id_adres_do, kraj_do, miejscowosc_do, adres_do, kod_do, tel_stac_do,
data, osob, fotelik, detale, ilosc_bagazu)
(SELECT 2, CURRENT_DATE, SUBSTRING_INDEX(USER(), '#', 1),
pasazerowie.id AS id_pasazera, pasazerowie.imie_nazwisko, pasazerowie.tel_1, pasazerowie.tel_2, pasazerowie.tel_3, pasazerowie.email,
odbiorcy.id AS id_odbiorcy, odbiorcy.imie_nazwisko AS imie_nazwisko2, odbiorcy.tel_1 AS tel2_1, odbiorcy.tel_2 AS tel2_2, odbiorcy.dokument AS dokument2,
adresy_z.id AS id_adres_z, adresy_z.kraj AS kraj_z, adresy_z.miejscowosc AS miejscowosc_z, adresy_z.adres AS adres_z, adresy_z.kod AS kod_z, adresy_z.tel_stac AS tel_stac_z,
adresy_do.id AS id_adres_do, adresy_do.kraj AS kraj_do, adresy_do.miejscowosc AS miejscowosc_do, adresy_do.adres AS adres_do, adresy_do.kod AS kod_do, adresy_do.tel_stac AS tel_stac_do,
NEW.data, NEW.osob, NEW.fotelik, NEW.detale, NEW.ilosc_bagazu
FROM pasazerowie
LEFT JOIN pasazerowie odbiorcy ON NEW.id_odbiorcy = odbiorcy.id
JOIN adresy adresy_do ON NEW.id_adres_do = adresy_do.id
JOIN adresy adresy_z ON NEW.id_adres_z = adresy_z.id
WHERE pasazerowie.id = NEW.id_pasazera);
END;

Related

SQL syntax error - multiple values from two tables & insert total

I need some help with the query below - I am trying to pull information regarding price and multiply with the quantity & insert the sum into the table. So far I have,
update passenger_baggage
SET passenger_baggage.total_baggage_cost=passenger_baggage.passenger_baggage_quantity*baggage_type.baggage_type_cost
FROM passenger_baggage INNER JOIN baggage_type
ON passenger_baggage.passenger_baggage_id = baggage_type.baggage_type_id
WHERE passenger_id = "3";
and getting this 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 'FROM passenger_baggage INNER JOIN baggage_type ON
passenger_baggage.passenge...' at line 3
Expecting the query to multiply the two values & insert the total.
There is no FROM clause in an UPDATE query.
Try with this modified query:
update passenger_baggage
INNER JOIN baggage_type
ON passenger_baggage.passenger_baggage_id = baggage_type.baggage_type_id
SET passenger_baggage.total_baggage_cost = passenger_baggage.passenger_baggage_quantity * baggage_type.baggage_type_cost
WHERE passenger_id = "3";
Try this:
UPDATE passenger_baggage, baggage_type
SET passenger_baggage.total_baggage_cost = passenger_baggage.passenger_baggage_quantity * baggage_type.baggage_type_cost
WHERE passenger_baggage.passenger_baggage_id = baggage_type.baggage_type_id AND passenger_id = "3";
Saw an example from the MySQL doc (https://dev.mysql.com/doc/refman/8.0/en/update.html)

Trying to perform divison using MySQL subqueries but getting syntax error

I am trying to get the value from MySQL subqueries but I am getting 'Error Code: 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 1'
The code is:
UPDATE national_avg_tariff SET Value= (CASE WHEN ((SELECT * FROM((SELECT Value
/ ((Select Sum_CPP_MlnRs from ppp_fy2020_2030_annex4.FY_CPP_exclKE_MlnRs where FiscalYear=2020) + (Select UoSC_MoF_exclKE_MlnRs from ppp_fy2020_2030_annex4.FY_UoSC_MoF_exclKE_MlnRs where FiscalYear=2020))/ (SELECT Sum(Sum_of_AvgMW) FROM ppp_fy2020_2030_annex4.AVG_OF_FY_MAX_DEMAND where FiscalYear=2020 GROUP BY FiscalYear)FROM tariff_true_cost.national_avg_tariff WHERE Sector='Residential' AND Category='Up to 50 Units' AND Parameter= 'Fixed Charge(Rs/kW/M)' AND FiscalYear=2020)temptbl8))>0)
THEN
(SELECT * FROM((SELECT Value
/ ((Select Sum_CPP_MlnRs from ppp_fy2020_2030_annex4.FY_CPP_exclKE_MlnRs where FiscalYear=2020) + (Select UoSC_MoF_exclKE_MlnRs from ppp_fy2020_2030_annex4.FY_UoSC_MoF_exclKE_MlnRs where FiscalYear=2020))/ (SELECT Sum(Sum_of_AvgMW) FROM ppp_fy2020_2030_annex4.AVG_OF_FY_MAX_DEMAND where FiscalYear=2020 GROUP BY FiscalYear)FROM tariff_true_cost.national_avg_tariff WHERE Sector='Residential' AND Category='Up to 50 Units' AND Parameter= 'Fixed Charge(Rs/kW/M)' AND FiscalYear=2020)temptbl8))
ELSE NULL END) where FiscalYear=2020 AND Parameter='Fixed Charge(%)';
The code doesn't show any red underline or pre-execution error something, the error only appears once I execute it.
Any help in rectifying syntax error of this piece of code would be appreciable. Thanks

#Error in SQL while updating table using join

I got an error while updating the table with the join query. The error message is:
Error Code: 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 'from employee details join employeedept on employee details.emp_dept=employeedept.' at line 3
Can anyone help me to resolve this error?
update employeeDetails
set employeeDetails.emp_age = employeeDetails.emp_age+20, employeeDetails.emp_salary = employeeDetails.emp_salary-500
from employeeDetails
join employeedept on employeeDetails.emp_dept = employeedept.dept_name
where employeedept.dept_name = 'CSE';
select * from employeeDetails;
List item
Have you tried using MySQL's UPDATE JOIN syntax yet? e.g.:
update employeeDetails
join employeeDept on employeeDetails.emp_dept = employeeDept.dept_name
set
employeeDetails.emp_age = employeeDetails.emp_age+20,
employeeDetails.emp_salary = employeeDetails.emp_salary-500
where employeeDept.dept_name = 'CSE';
select * from employeeDetails;

Update records with JOIN command

I have the following query that works great and shows me where the tbl_staff.staff_id and tbl_lead.rlog_create_user_id values do not match.
SELECT
tbl_staff.staff_id,
tbl_staff.username,
tbl_lead.rlog_create_user_name,
tbl_lead.rlog_create_user_id
FROM
tbl_staff
JOIN tbl_lead ON tbl_staff.username = tbl_lead.rlog_create_user_name
AND tbl_staff.staff_id <> tbl_lead.rlog_create_user_id;
The query returns values like this where you can see the 1014 does not match the 1004.
1014 bubba bubba 1004
I want to update the value in the tbl_lead.rlog_create_user_id to the same value as is found in tbl_staff.staff_id.
I tried to insert a SET command but it's giving me a generic syntax error:
SELECT
tbl_staff.staff_id,
tbl_staff.username,
tbl_lead.rlog_create_user_name,
tbl_lead.rlog_create_user_id
FROM
tbl_staff
JOIN tbl_lead ON tbl_staff.username = tbl_lead.rlog_create_user_name
AND tbl_staff.staff_id <> tbl_lead.rlog_create_user_id
SET tbl_lead.rlog_create_user_id=tbl_staff.staff_id ;
The actual error is:
[Err] 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 'SET tbl_lead.rlog_create_user_id=tbl_staff.staff_id' at line
10
I tried to change the SELECT to and UPDATE command using this question but still could not get it working: How can I do an UPDATE statement with JOIN in SQL?
Try this
UPDATE tbl_lead
JOIN tbl_staff ON tbl_staff.username = tbl_lead.rlog_create_user_name
SET tbl_lead.rlog_create_user_id = tbl_staff.staff_id
WHERE tbl_staff.staff_id <> tbl_lead.rlog_create_user_id;
Have you tried like this ?
UPDATE tbl_staff, tbl_lead
SET tbl_lead.rlog_create_user_id = tbl_staff.staff_id
WHERE tbl_staff.username = tbl_lead.rlog_create_user_name

Syntax error when updating columns

I'm trying to use the following code to update columns:
UPDATE user_profiles
SET range = '20', colResize = 'flex'
WHERE uid='472';
I get the following error:
Failed to execute SQL : SQL UPDATE user_profiles SET range = '20', colResize = 'flex' WHERE uid='472'; failed : 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 'range = '20', colResize = 'flex' WHERE uid='472'' at line 1
What am I overlooking?
range is a reserved keyword in MySQL. Use backticks to escape the name or use another column name.
SET `range` = 20