nermif 发布的文章

正数转负数

$number = 99;
$number *= -1;
echo $number;   //输出 -99

负数转正数

$number = -11;
$result = abs($number);
echo $result;   //输出 11

封装成方法使用

function plus_minus_conversion($number = 0){
    return $number > 0 ? -1 * $number : abs($number);
}

echo plus_minus_conversion(100.58);

cygwin方向键乱码和删除键失效解决

echo "set nocp" >> ~/.virc 解决方向键生效问题;
echo "set bs=2" >> ~/.virc 解决删除键(退格键)失效问题;

如果你的不是vi而是vim的话,请把输出文件名.virc换成.vimrc

let sugurl = "https://suggestion.baidu.com/su?wd=#content#&cb=window.baidu.sug",
                    script = document.createElement("script");
                sugurl = sugurl.replace("#content#", config.serarchValue);
                script.src = sugurl;
                document.getElementsByTagName("head")[0].appendChild(script);
                window.baidu = {
                    sug: function (json) {
                        if (json.s.length > 0) {
                            let d = [];
                            for (let i in json.s) {
                                d.push({keywords: json.s[i]});
                                if (i == (config.searchlimit - 1)) break;
                            }
                            config.searchData.list = d;
                            template();
                        }
                    }
                };

由于typeof只能判断5中基本类型:即 “number”,”string”,”undefined”,”boolean”,”object”

对于数组、函数、对象来说并不能详细的检测出类型, 这里使用的是对象的一个原生扩展函数,用来更精确的区分数据类型。
详细的判断类型给出如下:

console.log(Object.prototype.toString.call("jerry"));//[object String]
console.log(Object.prototype.toString.call(12));//[object Number]
console.log(Object.prototype.toString.call(true));//[object Boolean]
console.log(Object.prototype.toString.call(undefined));//[object Undefined]
console.log(Object.prototype.toString.call(null));//[object Null]
console.log(Object.prototype.toString.call({name: "jerry"}));//[object Object]
console.log(Object.prototype.toString.call(function(){}));//[object Function]
console.log(Object.prototype.toString.call([]));//[object Array]
console.log(Object.prototype.toString.call(new Date));//[object Date]
console.log(Object.prototype.toString.call(/\d/));//[object RegExp]
console.log(Object.prototype.toString.call(new Person));//[object Object]

由此可进一步封装成自己需要的方法,需要时调用。

getType: function(j){
    let type = Object.prototype.toString.call(j) , ret;
    switch(type){
        case '[object Function]':
            ret = 'function';
        break;
        case '[object String]':
            ret = 'string';
        break;
        case '[object Number]':
            ret = 'number';
        break;
            case '[object Object]':
        ret = 'object';
            break;
        case '[object Array]':
            ret = 'array';
        break;
        default:
            ret = false;
    }
    return ret;
},

测试类型

let data = ['xx','aa'];
console.log(this.getType(data))
// array

如何获取在单表中把两个字段运算后统计行数据

以往的字段间统计,查询使用sum函数求出两个字段运算的结构,再赋予新的别名作为新列

SELECT SUM(`market_price` - `shop_price`) AS 'price_amount' FROM `tp_goods` GROUP BY `goods_id`;

然后统计单个字段列最大值是这样的

SELECT MAX(`price_amount`) FROM (xxx) b;

由于第一条查询语句查询出来的数据相当是新表;再由得出组合语句

SELECT MAX(`price_amount`) FROM (SELECT SUM(`market_price` - `shop_price`) AS 'price_amount' FROM `tp_goods` GROUP BY `goods_id`) b;
查询花费 0.0006 秒

为查询中内嵌查询
更为高级的写法,在大数据量中查询时间更短,下面执行时间花费 0.0004 秒

SELECT
  `a`.`market_price` - `a`.`shop_price` AS 'discount'
FROM
  `tp_goods` a
    INNER JOIN `tp_goods_zh` AS b ON `a`.`goods_id` = `b`.`goods_id`
    INNER JOIN `tp_auction` AS c ON `a`.`goods_id` = `c`.`goods_id`
WHERE
  `sale_type` = 5 AND `start_time` BETWEEN 1551196800 AND 1551283200
LIMIT 1

ThinkPHP版本多写法为

$goods
   ->alias('a')
   ->join("$lang AS b ON a.goods_id=b.goods_id")
   ->join('__AUCTION__ AS c ON a.goods_id = c.goods_id')
   ->where(array('sale_type' => 5, 'start_time' => array('between', array($today, $todayEnd))))
   ->field('a.market_price - a.shop_price as discount')
   ->find();