温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

nodejs Sequelize和mysql有哪些常用的查询命令

发布时间:2020-05-23 17:36:45 来源:网络 阅读:229 作者:三月 栏目:MySQL数据库

下面讲讲关于nodejs Sequelize和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 秒钟内没有被使用过的话,那么就释放线程
    }

});

对于以上nodejs Sequelize和mysql有哪些常用的查询命令相关内容,大家还有什么不明白的地方吗?或者想要了解更多相关,可以继续关注我们的行业资讯板块。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI