nodejs 操作mysql数据库遍历数据

需求: 查询文章列表 并把用户头像获取到

思路: 1先查询文章数据 然后根据数据中的id 重新查询用户表 获取用户头像

一开始是循环数据库查询 发现遍历query查询数据 不同步 百度后 使用 然后使用async 最后获取用户头像 

npm i asynv --save 

后来感觉方法笨拙 遍历查询性能等等都不好 请教一下比人 直接上图  注释是第一次写的 没注释是更改后的

db.query(`select * from article order by article.datetime desc limit ${post.num},${post.num+10}`,(err,result)=>{
      if(err) throw err;

      //db.query(`select * from article order by article.datetime desc limit ${post.num},${post.num+10}`  通过前端num +10 查询10条按时间排列的最新数据
      // async.each(result, function (item, callback) {
      //   db.query(`select photo from userName where openid="${item.openid}"`,(err,data)=>{
      //     if(err) throw err;
      //     // console.log('qq',data[0].photo)
      //     item.photo = data[0].photo;
      //     item.picture = item.picture.split('|')[0].replace(/\s*/g,"")
      //     item.pic=item.picture.substring(50, item.picture.length)
      //     console.log('picture',item.pic)
      //     callback(null)
      //   })
      // }, function (err) {
      //   console.log(err)
      //   return res.send({status: true,result: result})
      // })
        // console.log(result)

        var num = "";     //这里数据库没有数组概念 所以把数组改成字符串 注意重点 字符串加"" 意思就是查询语句一定是 where in ("ccc","vvvv") 这种 console一下注意下一下
        result.forEach((item,index) => {
          console.log(item.openid)
          if(index == result.length -1){
            num += '"'+item.openid+ '"'
            return;
          }
          num += '"'+item.openid+ '"' + ","
        })
        num += ","+ "'cc'"
        // console.log(`select photo from userName where openid in (${num})`)  根据条件查询数据
        db.query(`select photo,openid from userName where openid in (${num})`,(err,data)=>{
          console.log(data)
          for(var i in result) {
            for(var j in data){
              if(result[i].openid == data[j].openid){
                result[i].photo = data[j].photo
                result[i].picture = result[i].picture.split('|')[0].replace(/\s*/g,"")
                break;
              }
            }
          }
          console.log(result)
        })
    });
  })

 

第二部还没有优化 操作比较多 但是一定比第一个好 因为

 

水平有限 不过功能没毛病 有问题 留言 看到就回答

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Node.js操作MySQL数据库的示例代码: 1.安装mysql模块 ```shell npm install mysql ``` 2.连接MySQL数据库 ```javascript const mysql = require('mysql'); const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'password', database: 'test' }); connection.connect((err) => { if (err) throw err; console.log('Connected!'); }); ``` 3.创建数据库 ```javascript const mysql = require('mysql'); const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'password' }); connection.connect((err) => { if (err) throw err; console.log('Connected!'); connection.query('CREATE DATABASE test', (err, result) => { if (err) throw err; console.log('Database created!'); }); }); ``` 4.创建表 ```javascript const mysql = require('mysql'); const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'password', database: 'test' }); connection.connect((err) => { if (err) throw err; console.log('Connected!'); const sql = 'CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))'; connection.query(sql, (err, result) => { if (err) throw err; console.log('Table created!'); }); }); ``` 5.插入数据 ```javascript const mysql = require('mysql'); const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'password', database: 'test' }); connection.connect((err) => { if (err) throw err; console.log('Connected!'); const sql = 'INSERT INTO customers (name, address) VALUES ("Company Inc", "Highway 37")'; connection.query(sql, (err, result) => { if (err) throw err; console.log('1 record inserted'); }); }); ``` 6.查询数据 ```javascript const mysql = require('mysql'); const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'password', database: 'test' }); connection.connect((err) => { if (err) throw err; console.log('Connected!'); const sql = 'SELECT * FROM customers'; connection.query(sql, (err, result) => { if (err) throw err; console.log(result); }); }); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值