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

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

#去除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 秒钟内没有被使用过的话,那么就释放线程
    }

});