Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Node.js MySQL 连接查询


连接两个或多个表

可以使用 JOIN 语句,根据两个或多个表之间相关的列,将它们的行组合在一起。

假设您有一个 "users" 表和一个 "products" 表

users

[
  { id: 1, name: 'John', favorite_product: 154},
  { id: 2, name: 'Peter', favorite_product: 154},
  { id: 3, name: 'Amy', favorite_product: 155},
  { id: 4, name: 'Hannah', favorite_product:},
  { id: 5, name: 'Michael', favorite_product:}
]

products

[
  { id: 154, name: 'Chocolate Heaven' },
  { id: 155, name: 'Tasty Lemons' },
  { id: 156, name: 'Vanilla Dreams' }
]

这两个表可以通过使用 users 的 favorite_product 字段和 products 的 id 字段来连接。

示例

选择两个表中都匹配的记录

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "yourusername",
  password: "yourpassword",
  database: "mydb"
});

con.connect(function(err) {
  if (err) throw err;
  var sql = "SELECT users.name AS user, products.name AS favorite FROM users JOIN products ON users.favorite_product = products.id";
  con.query(sql, function (err, result) {
    if (err) throw err;
    console.log(result);
  });
});
运行示例 »

注意:您可以使用 INNER JOIN 代替 JOIN。它们都会给出相同的结果。

将以上代码保存到名为 "demo_db_join.js" 的文件中,然后运行该文件

运行 "demo_db_join.js"

C:\Users\您的用户名>node demo_db_join.js

这将给出以下结果

[
  { user: 'John', favorite: 'Chocolate Heaven' },
  { user: 'Peter', favorite: 'Chocolate Heaven' },
  { user: 'Amy', favorite: 'Tasty Lemons' }
]

从以上结果可以看出,只返回了两个表中都匹配的记录。



左连接

如果要返回所有用户,无论他们是否有喜欢的产品,请使用 LEFT JOIN 语句

示例

选择所有用户及其喜欢的产品

SELECT users.name AS user,
products.name AS favorite
FROM users
LEFT JOIN
products ON users.favorite_product = products.id
运行示例 »

这将给出以下结果

[
  { user: 'John', favorite: 'Chocolate Heaven' },
  { user: 'Peter', favorite: 'Chocolate Heaven' },
  { user: 'Amy', favorite: 'Tasty Lemons' },
  { user: 'Hannah', favorite: null },
  { user: 'Michael', favorite: null }
]

右连接

如果要返回所有产品,以及将它们作为自己喜欢的用户,即使没有用户将它们作为自己喜欢的,请使用 RIGHT JOIN 语句

示例

选择所有产品以及将它们作为自己喜欢的用户

SELECT users.name AS user,
products.name AS favorite
FROM users
RIGHT JOIN
products ON users.favorite_product = products.id
运行示例 »

这将给出以下结果

[
  { user: 'John', favorite: 'Chocolate Heaven' },
  { user: 'Peter', favorite: 'Chocolate Heaven' },
  { user: 'Amy', favorite: 'Tasty Lemons' },
  { user: null, favorite: 'Vanilla Dreams' }
]

注意:没有喜欢的产品的 Hannah 和 Michael 未包含在结果中。


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.