简体   繁体   中英

How to use MySQL @ Session Variables in NodeJS?

I'm creating an app with purpose to show some charts. I built with Express Framework (NodeJS) and Sequelize for the mysql database connecter. I almost done but I getting stuck with this query, because the query requires me to use @ Session Variables.

The following code is my query that I use

SELECT 
drive_id, officer_id, officer_name, 
vehicle_serial_number, 
FORMAT(ROUND(ROUND(SUM(TIME_TO_SEC(TIMEDIFF(finish_time, start_time)) / 60)) / 3), 1) AS average_cycle_time,
ROUND(SUM(TIME_TO_SEC(TIMEDIFF(finish_time, start_time)) / 60)) AS cycle_minute,
COUNT(drive_id) AS amount_of_cycle, 
drive_rank,
cycle_date
FROM (
SELECT
c.drive_id,
o.id AS officer_id,
v.serial_number AS vehicle_serial_number,
c.start_time, c.finish_time,
c.date AS cycle_date,
@drive_rank := IF(@current_drive = o.name, @drive_rank + 1, 1) AS drive_rank, 
@current_drive := o.name AS officer_name
FROM cycles c
INNER JOIN drives d ON d.id = c.drive_id
INNER JOIN officers o ON o.id = d.officer_id
INNER JOIN vehicles v ON v.id = d.vehicle_id
WHERE c.date = '2019-05-01'
ORDER BY c.drive_id ASC
) ranked
WHERE ranked.drive_rank <= 3
GROUP BY drive_id, officer_id;

That result query is actually works, but @drive_rank doesn't run. Is there a way for me to execute that query OR I must to rethink to rearrange the query? Thank you.

Ok, I've got the answer for my own question, lol. I want to share the answer if later someone has the same problem, so there it is:

  1. We can't use Sequelize if want to use MySQL @ Session Variable. Use standard mysql
  2. Then execute the query twice sequentially

For my case, it'll works, good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM