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

下面讲讲关于nodejs Sequelize和MySQL有哪些常用的查询命令,文字的奥妙在于贴近主题相关。所以,闲话就不谈了,我们直接看下文吧,相信看完nodejs Sequelize和mysql有哪些常用的查询命令这篇文章你一定会有所受益。

创新互联是专业的柳林网站建设公司,柳林接单;提供成都网站设计、成都网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行柳林网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

#去除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有哪些常用的查询命令相关内容,大家还有什么不明白的地方吗?或者想要了解更多相关,可以继续关注我们的行业资讯板块。

本文标题:nodejsSequelize和mysql有哪些常用的查询命令
文章转载:https://www.cdcxhl.com/article40/gpegho.html

成都网站建设公司_创新互联,为您提供App开发网页设计公司网站设计手机网站建设微信公众号服务器托管

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

小程序开发