使用Express Node进行多个mysql查询

时间:2018-11-22 16:33:15

标签: mysql node.js express

我正在运行Express服务器并连接到MySQL数据库。我有两个通过user_id引用的表。当用户提供电子邮件地址时,我需要查询users来找到user_id,然后运行另一个查询来加入tracking表。实现此目标的最佳方法是什么?还要感谢代码中任何部分的最佳实践建议!

谢谢

MySQL

CREATE TABLE users (
  user_id INT NOT NULL AUTO_INCREMENT UNIQUE,
  first_name VARCHAR(100) NOT NULL,
  last_name VARCHAR(100) DEFAULT NULL,
  email VARCHAR(320) NOT NULL UNIQUE,
  date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (user_id)
);

CREATE TABLE tracking (
  tracking_id INT NOT NULL AUTO_INCREMENT,
  user_id INT NOT NULL,
  metric VARCHAR(100) NOT NULL,
  unit VARCHAR(100) NOT NULL,
  amount DOUBLE DEFAULT NULL,
  date_tracked DATE,
  date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (tracking_id),
  INDEX user_ind (user_id),
  FOREIGN KEY (user_id)
   REFERENCES users(user_id)
   ON DELETE CASCADE
);

Express(Nodejs)

const express = require("express");
const app = express();
const port = 3000;
var mysql = require("mysql");
var connection = mysql.createConnection({
  host: "",
  user: "",
  password: "",
  database: "tracker"
});

app.get("/data/:email/metric/:metric", (req, res) => {
  console.log(req.params);
  let user_id = "";

  connection.connect();

  connection.query(
    `SELECT user_id FROM users WHERE email = '${req.params.email}';`,
    function(error, results, fields) {
      if (error) throw error;
      user_id = results[0].user_id;
    }
  );

  connection.query(
    `SELECT users.first_name, users.last_name, users.email, tracking.metric, tracking.unit, tracking.amount, tracking.date_tracked FROM users JOIN tracking ON users.user_id = tracking.user_id WHERE users.user_id = '${user_id}' AND metric = '${
      req.params.metric
    }';`,
    function(error, results, fields) {
      if (error) throw error;
      res.send(results);
    }
  );

  connection.end();
});

app.listen(port, () => console.log(`Example app listening on port ${port}!`));

2 个答案:

答案 0 :(得分:1)

您可以通过一个查询来完成此操作,只需JOIN上的表user_id

SELECT 
  u.first_name, 
  u.last_name, 
  u.email, 
  t.metric, 
  t.unit, 
  t.amount, 
  t.date_tracked 
FROM users u
INNER JOIN tracking AS t ON t.user_id = u.user_id
WHERE u.email = '<email>' AND t.metric = '<metric>'

此外,FWIW您应该使用参数化查询来保护自己免受SQL注入

const values = [
  req.params.email, 
  req.params.metric
];
connection.query(
  'SELECT ... WHERE u.email = ? AND t.metric = ?', 
  values, 
  (err, results, fields) => {
    if (err) return next(e); // propagate the error to Express, rather than throwing in the callback

    res.send(results);
  }
);

答案 1 :(得分:1)

  

我需要查询用户以找到user_id,然后运行另一个查询以加入跟踪表。实现此目标的最佳方法是什么?

您只需一个查询即可实现:

    SELECT users.first_name, users.last_name, users.email, 
            tracking.metric, tracking.unit, tracking.amount,
            tracking.date_tracked 
    FROM users 
    JOIN tracking 
    ON users.user_id = tracking.user_id 
    WHERE users.email = '${req.params.email}' AND 
          metric = '${req.params.metric}
  

对于代码的任何部分的最佳实践建议,我们也表示赞赏!

首先,应该在服务器启动后连接到mySql。

在每个请求上连接和断开连接都会影响性能(打开套接字并在外部连接是一项昂贵的任务)

所以尝试类似的东西:

    CONNECTION.connect().then(() => {
        // mysql is ready , let's start express server 
        // accepting connections
        app.listen(port, () => console.log(`ready on ${port}!`));
    }).catch(console.error)

您应该知道的另一个好的做法是并行运行查询 与Promise.all([])。当您需要运行多个不相关的查询时, 您可以一个接一个地执行它们,使所有结果的 total 等待时间大大缩短。