I want to implement the following sql query:
date_count = await customQuery(
`
SELECT
COUNT(org.name
OR c.name
OR co.name
OR seg.arrival_details
OR seg.departure_details
OR seg.segment_airline_id
OR seg.segment_arrival_airport_code
OR seg.segment_departure_airport_code
OR seg.segment_flight_number
OR seg.trip_id
OR b.trip_id
OR bf.pnr_no
OR bf.airline_pnr_no
OR b.booking_status
OR b.created
OR org.organisation_id
OR org.name
) AS total_count
FROM
segment_details AS seg
LEFT JOIN trip_details AS tr ON seg.trip_id = tr.trip_id
LEFT JOIN bookings_flight AS bf ON tr.bookings_flight_id = bf.booking_flight_id
LEFT JOIN bookings AS b ON bf.booking_id = b.booking_id
LEFT JOIN organisation AS org ON b.organisation_id = org.organisation_id
LEFT JOIN city AS c ON org.city_id = c.city_id
LEFT JOIN country AS co ON org.country_id = co.country_id
WHERE
seg.segment_departure_date BETWEEN
${moment(date.startDate).startOf('day').format("YYYY-MM-DD HH:mm:ss") }
AND ${moment(date.endDate).endOf('day').format("YYYY-MM-DD HH:mm:ss")}
`)
But I am getting the following error:
UnhandledPromiseRejectionWarning: Error: ER_PARSE_ERROR: 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 '00:00:00
my date result comes as :
'2021-10-01T08:00:00.000Z'
and when I apply moment js to it my result is as follow:
'2021-10-01 00:00:00'
I am using MySQL, SQL Yog
How can I fix this error? Any help or suggestion will be appreciated.
Try to surround your dates with single quotes:
...
seg.segment_departure_date BETWEEN
'${moment(date.startDate).startOf('day').format("YYYY-MM-DD HH:mm:ss")}'
AND '${moment(date.endDate).endOf('day').format("YYYY-MM-DD HH:mm:ss")}'
Try it out. You need to add single quotes
const startDate = moment(date.startDate).startOf('day').format("YYYY-MM-DD HH:mm:ss");
const endDate = moment(date.endDate).endOf('day').format("YYYY-MM-DD HH:mm:ss");
...
WHERE
seg.segment_departure_date BETWEEN '${startDate}' AND '${endDate}'
Related
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;
I am trying to update a table in my database from another table. Here is my Syntax, but I can't seem to find any issues with it. I keep getting SQL Error (1064).
UPDATE customers b
SET customers.takerid = customer_update_2016.ot
FROM customer_update_2016 a, customers b
WHERE a.phone = b.phone && a.lname = b.lname
SQL 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 'FROM customer_update_2016 a, customers b WHERE a.phone = b.phone & a.lname =b' at line 3
Solution:
UPDATE customers
INNER JOIN customer_update_2016
ON customers.phone = customer_update_2016.phone
AND customers.lname = customer_update_2016.lname
SET customers.takerid = customer_update_2016.ot
you have both mysql and sql-server
which one?
UPDATE customers
SET customers.takerid = customer_update_2016.ot
FROM customers
JOIN customer_update_2016
on customers.phone = customer_update_2016.phone
and customers.lname = customer_update_2016.lname
and customers.takerid <> customer_update_2016.ot
Follow something like this always
UPDATE [table1_name] AS t1
INNER JOIN [table2_name] AS t2
ON t1.[column1_name] = t2.[column1_name]
SET t1.[column2_name] = t2.[column2_name];
To solve your problem you should use JOIN, in my case I had to pass data from one table to another like you and this was the way that solved my problem, hope it help with yours and others.(I'm using MariaDB v10.4)
UPDATE
table1
LEFT JOIN table2 AS tb2
ON tb2.fieldThatJoin = table1.fieldThatJoin//this part indicate the field that join them
SET
table1.field1 = tb2.field1;
//if you want to update more than one field then do this
table1.field1 = tb2.field1,
table1.field2 = tb2.field2,
table1.field3 = tb2.field3;//remember to put the ';' at the end
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
I'm getting this following error:
MYSQL 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 'DISTINCT R.roomtype as roomtype , I1.meal_plan as
mealplan , I1.`bed_t' at line 3
My Code is
SELECT
DISTINCT H.`hotelname` as `hotelname` ,
DISTINCT R.`roomtype` as `roomtype` ,
I1.`meal_plan` as `mealplan` ,
I1.`bed_type` as `bedtype` ,
I1.`roomrate` as `roomrate` ,
I1.`pax` as `pax` ,
I1.`childrate` as `childrate` ,
I1.`childrate1` as `childrate1` ,
I1.`childrate2` as `childrate2` ,
I1.`childrate3` as `childrate3` ,
P.`profitmarkup_type` as `profit_type` ,
P.`applyprofit_val_room` as `applyprofit_val_room` ,
P.`applyprofit_val_bed` as `applyprofit_val_bed`
FROM `hoteldetails` H
INNER JOIN `roomdetails` R
on H.`hotelname` = R.`hotelname`
INNER JOIN `inventorypolicy` I1
on I1.`hotel_name` = H.`hotelname`
AND I1.`room_plan` = R.`roomtype`
AND I1.`bed_type`=R.`bedtype`
AND I1.`meal_plan`=R.`mealplan`
AND I1.`suppliername`=H.`supplier`
INNER JOIN `profitmarkup` P
on P.`hotel_name` = H.`hotelname`
AND P.`suppliername` = H.`supplier`
WHERE H.`active`='1'
AND H.`country`='MAL'
AND H.`city`='KL'
AND H.`show2web`='1'
AND H.`expiry_date` >= 11/09/2014
AND I1.`inventory_status`='1'
AND P.`markup_status` = '1'
AND 20140911 BETWEEN ((I1.`date1`)+(I1.`month1`*100)+(I1.`year1`*10000)) AND ((I1.`date2`)+(I1.`month2`*100)+(I1.`year2`*10000))
AND 20140911 BETWEEN ((P.`date1`)+(P.`month1`*100)+(P.`year1`*10000)) AND ((P.`date2`)+(P.`month2`*100)+(P.`year2`*10000))
I dont know how to fix this. Any help is greatly appreciated.
As you can learn from the documentation of the SELECT MySQL statement, DISTINCT can be placed before any field or expression to be selected. This automatically forces a single instance of it in a query.
And somewhere down the road there is a sentence that says everything:
DISTINCTROW is a synonym for DISTINCT.
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