You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.8 KiB
61 lines
1.8 KiB
// 查询特定名称商品的创建者
|
|
|
|
const dotenv = require('dotenv');
|
|
const mysql = require('mysql2/promise');
|
|
const path = require('path');
|
|
|
|
// 加载环境变量
|
|
dotenv.config({ path: path.resolve(__dirname, '.env') });
|
|
|
|
// 数据库连接配置
|
|
const dbConfig = {
|
|
host: process.env.DB_HOST || 'localhost',
|
|
port: process.env.DB_PORT || 3306,
|
|
user: process.env.DB_USER || 'root',
|
|
password: process.env.DB_PASSWORD || '',
|
|
database: process.env.DB_NAME || 'wechat_app',
|
|
timezone: '+08:00' // 设置时区为UTC+8
|
|
};
|
|
|
|
async function findProductCreator() {
|
|
let connection;
|
|
try {
|
|
// 连接数据库
|
|
connection = await mysql.createConnection(dbConfig);
|
|
console.log('数据库连接成功');
|
|
|
|
// 查询名称为88888的商品及其创建者
|
|
const [products] = await connection.query(`
|
|
SELECT p.productId, p.productName, p.sellerId, u.userId, u.nickName, u.phoneNumber
|
|
FROM products p
|
|
LEFT JOIN users u ON p.sellerId = u.userId
|
|
WHERE p.productName = '88888'
|
|
`);
|
|
|
|
if (products.length === 0) {
|
|
console.log('未找到名称为88888的商品');
|
|
return;
|
|
}
|
|
|
|
console.log(`找到 ${products.length} 个名称为88888的商品:`);
|
|
products.forEach((product, index) => {
|
|
console.log(`\n商品 ${index + 1}:`);
|
|
console.log(` 商品ID: ${product.productId}`);
|
|
console.log(` 商品名称: ${product.productName}`);
|
|
console.log(` 创建者ID: ${product.sellerId}`);
|
|
console.log(` 创建者昵称: ${product.nickName || '未设置'}`);
|
|
console.log(` 创建者手机号: ${product.phoneNumber || '未设置'}`);
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error('查询失败:', error.message);
|
|
} finally {
|
|
if (connection) {
|
|
await connection.end();
|
|
console.log('\n数据库连接已关闭');
|
|
}
|
|
}
|
|
}
|
|
|
|
// 执行查询
|
|
findProductCreator();
|