nodejs mysql查询语句_nodejs Sequelize 简单查询语句和 mysql常用的几个查询命令

我是前端,但总有需求让做后端的活,所以顺带着熟悉了下简单的查询语句

贴出来,如果有需要可以参考下,备注很详细,就不多解释了

废话不多说贴代码:

#去除unionid 重复的搜索结果

#query_resultsign 表名

select *, count(unionid) from query_resultsign where issign='false' group by unionid ;

#去除unionid 重复的搜索结果

#query_resultsign 表名

select *, count(unionid) from query_resultsign where issign='true' group by unionid ;

#求未签约用户的平均访问频率(即为求搜索结果列的平均值issign='false' 未签约)

#cuid 是unid的别名

#query_resultsign 表名

select AVG(bs.cuid) as unUserAvg FROM (select *, count(unionid) cuid from query_resultsign where issign='false' group by unionid ) as bs;

#求平均值

#(即为求搜索结果issign='true' count的平均值)

#bs为子查询的别名,不带别名会报错

#query_resultsign 表名

select AVG(bs.cuid) userAvg FROM (select *, count(unionid) cuid from query_resultsign where issign='true' group by unionid ) as bs;

#增加id 列 int

#query_resultsign

ALTER TABLE query_resultsign add id int;

#使表 query_resultsign (上一步)增加的列变为自增列

alter table query_resultsign change id id int NOT NULL AUTO_INCREMENT primary key;

#获取两列数据中有相同数据的列

#query_resultsign 表名

select p1.* from query_resultsign p1,query_resultsign p2 where p1.id<>p2.id

and p1.x = p2.x

and p1.y = p2.y ;

#查找表query_resultsign unionid 相同的用户

select p1.* from query_resultsign p1,query_resultsign p2 where p1.id<>p2.id

and p1.unionid = p2.unionid ;

sequelize 的调用sql语句的方法顺带提一下,网上大多教程都是用model 查询的,每次都要建立model。有点麻烦 。配置的教程请参看配置教程。

sequelize调用sql主要用query(sql,{})方法:

var Sequelize = require('sequelize');//引入sequelize

var sequelize = require('./../../database/dataconfig'); //引入连接配置文件

//查找签约用户

exports.selectHeatData = function (req, res) {

return sequelize.query("select * from `query_resultSign` where issign ='true'", { type: sequelize.QueryTypes.SELECT }).then(data => {

// console.log('******', data);

res.send(data);

}).catch(err => {

console.log('错误', err)

})

}

//其他方法就是换了下sql语句

主要知识点就是query方法内传入查询出的结果的类型 { type: sequelize.QueryTypes.SELECT } 这样就不用手动转换成json对象了。

附带配置文件代码

dataconfig.js

var Sequelize = require('sequelize');

module.exports = new Sequelize('数据库名', '用户名', '登录密码', {

host: 'localhost', // 数据库地址

dialect: 'mysql', // 指定连接的数据库类型

operatorsAliases: false,

pool: {

max: 5, // 连接池中最大连接数量

min: 0, // 连接池中最小连接数量

idle: 10000 // 如果一个线程 10 秒钟内没有被使用过的话,那么就释放线程

}

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值