#Error in SQL while updating table using join - mysql

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;

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)

With request in sql doesn't work & recognize

I have a problem with my request, the request doesn't recognized WITH(). I work on Ubuntu & MySQL, can I get help please!
DB::select(DB::raw("WITH liststudentsoff as (
SELECT tbl_etudiants.*, tbl_cours.idCours FROM dependance_groupes
inner join tbl_etudiants on tbl_etudiants.idStudent = dependance_groupes.studentID
inner join tbl_cours on tbl_cours.groupID = dependance_groupes.groupID
inner join tbl_formations on tbl_formations.idFormation = tbl_cours.formationID
)
SELECT * FROM liststudentsoff
LEFT JOIN tbl_absences ON liststudentsoff.idStudent = tbl_absences.studentID AND liststudentsoff.idCours = tbl_absences.coursID
WHERE liststudentsoff.idCours = ".$request->idCours." and liststudentsoff.formationID = ".$cours->formationID.""));
Error display:
[previous exception] [object] (PDOException(code: 42000): SQLSTATE[42000]: Syntax error or access violation: 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 'liststudentsoff as (
SELECT tbl_etudiants.*, tbl_cours.idCours FROM ' at line 1 at /var/www/html/emarge-backoffice/vendor/laravel/framework/src/Illuminate/Database/Connection.php:338)

#1064 - You have an error in your SQL syntax; ....." ...INNER JOIN"

SELECT
COUNT(IPD_Admit_Register_Det.BID) AS BID,
Bed_WardList.WID, Bed_FloorList.FID
FROM
Bed_BedList
INNER JOIN shift_last_bed
INNER JOIN IPD_Admit_Register_Det
ON shift_last_bed.IPDid = IPD_Admit_Register_Det.IPDid
AND shift_last_bed.IDPdid = IPD_Admit_Register_Det.IPDdid
ON Bed_BedList.BID = IPD_Admit_Register_Det.BID
INNER JOIN Bed_FloorList
INNER JOIN Bed_WardList
ON Bed_FloorList.FID = Bed_WardList.FID
ON Bed_BedList.WID = Bed_WardList.WID
GROUP BY
IPD_Admit_Register_Det.BID,
Bed_WardList.WID,
Bed_FloorList.FID
above query is execute successfully in mssql server 2008 but when same doing in MySql using phpMyAdmin it throws error like
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 'ON Bed_BedList.BID = IPD_Admit_Register_Det.BID INNER JOIN
Bed_FloorList INNER J' at line 5
Guys Appreciate your help! finally I got proper query as below
SELECT
COUNT(IPD_Admit_Register_Det.BID) AS BID,
Bed_WardList.WID, Bed_FloorList.FID
FROM
shift_last_bed,
IPD_Admit_Register_Det,
Bed_BedList,
Bed_FloorList,
Bed_WardList
WHERE
(shift_last_bed.IPDid = IPD_Admit_Register_Det.IPDid)
AND (shift_last_bed.IPDdid = IPD_Admit_Register_Det.IPDdid)
AND (Bed_BedList.BID = IPD_Admit_Register_Det.BID)
AND (Bed_FloorList.FID = Bed_WardList.FID)
AND (Bed_BedList.WID = Bed_WardList.WID)
GROUP BY
IPD_Admit_Register_Det.BID,
Bed_WardList.WID,
Bed_FloorList.FID

SQL error #1064 when nesting a SELECT MAX in a LEFT JOIN

Please let me know why I get an error on the following SQL (I am using a MySQL database) and how to rectify it:
SELECT at_award_description.ad_id,
at_award_description.ad_detail,
at_award_description.ad_archived_date,
a.cad_id,
a.cad_task_completion_date
FROM at_award_description
LEFT JOIN at_cub_award_date AS a ON ((at_award_description.ad_id = a.ad_id)
AND (a.ca_id = 37)
AND a.cad_task_completion_date = (SELECT MAX(b.cad_task_completion_date)
FROM at_cub_award_date AS b
WHERE a.ca_id = b.ca_id AND a.ad_id = b.ad_id
)
)
WHERE at_award_description.aw_id = 5
ORDER BY at_award_description.ad_order;
I broke the SQL down to ensure each part worked.
SELECT MAX(b.cad_task_completion_date)
FROM at_cub_award_date AS b
And
SELECT at_award_description.ad_id,
at_award_description.ad_detail,
at_award_description.ad_archived_date,
a.cad_id,
a.cad_task_completion_date
FROM at_award_description
LEFT JOIN at_cub_award_date AS a ON ((at_award_description.ad_id = a.ad_id)
AND (a.ca_id = 37)
)
WHERE at_award_description.aw_id = 5
ORDER BY at_award_description.ad_order;
Each of these work on their own. When I combine them I get the following error:
Error
SQL query:
LIMIT 0, 25
MySQL said:
#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 'LIMIT 0, 25' at line 1
Regards,
Glyn
EDIT
#Barmar came up with the solution to remove the ";" at the end when testing. However, the result was not what I expected so I ended up changing this to:
SELECT at_award_description.ad_id,
at_award_description.aw_id,
at_award_description.ad_group,
at_award_description.ad_detail,
at_award_description.ad_start_date,
at_award_description.ad_archived_date,
MAX(at_cub_award_date.cad_task_completion_date)
FROM at_award_description
LEFT JOIN at_cub_award_date ON ((at_award_description.ad_id = at_cub_award_date.ad_id)
AND (at_cub_award_date.ca_id = 37))
WHERE at_award_description.aw_id = 5
GROUP BY at_award_description.ad_group
ORDER BY at_award_description.ad_order, at_award_description.ad_group

SQL UPDATE with SELECT Issue. SQL syntax error

I have a problem with my SQL. I would like to use a select to make an update, but so far I haven't been able to acomplish it. I would really apreciate if anyone could please help me. I'm quite new at MySQL. THe query in question is this:
UPDATE
pac
SET
pac.pac_spc_id = dtn_atual.spc_id
FROM
pac_paciente pac
INNER JOIN ( SELECT
dtn.dtn_pac_id,
spc_id
FROM `pac_destino` dtn
INNER JOIN ( select
des.dtn_pac_id,
MAX(CONCAT(des.dtn_data, des.dtn_seq)) AS datamax
FROM pac_destino des
GROUP BY des.`dtn_pac_id`
) AS d1 ON d1.dtn_pac_id = dtn.dtn_pac_id
AND d1.datamax = CONCAT(dtn.dtn_data, dtn.dtn_seq)
LEFT JOIN pac_classdestino c1 ON c1.cdo_id = dtn.dtn_cdo_id
LEFT JOIN pac_statuspaciente spc ON spc.spc_id = c1.cdo_spc_id
) AS dtn_atual on dtn_atual.dtn_pac_id = pac.`pac_id`
The error generated 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 'FROM pac_paciente pac INNER JOIN ( SELECT
dtn.dtn_pac_id, spc_id
' at line 5