sql中!=的陷阱

mysql/SQL server有,clickhouse也有,hive未验证。 null 值的比较这里另外说下 SQL 里 null 值的比较,任何与 null 值的比较结果,最后都会变成 null,以PostgreSQL为例,如下:select null != null; select null = null; select null > 1; select null <> 1; 以上结果都是 null,而不是什么 true 或者 false。另外有些函数是不支持 null 值作为输入参数的,比如count()或者sum()等。 在写 SQL 条件语句时经常用到 不等于 != 的筛选条件。此时要注意此条件会将字段为 Null 的数据也当做满足不等于的条件而将数据筛选掉。(也就是说会忽略过滤掉为 null 的数据,导致数据不准确)。 SELECT * FROM A WHERE B1 != 1 不会查出null值 修改: SELECT * FROM A WHERE B1 != 1 OR B1 is Null SELECT * FROM A WHERE IFNULL(B1,'') != 1 为什么会这样呢?这还得从 mysql 的底层开始说起,因为 NULL 不是一个「值」,而是「没有值」。 「没有值」不满足「值不等于1」这个条件,怎么解决可以使用 ifnull() 方法,将 null 转为空字符串,或者这个字段做出判断 加上OR a is null。当然使用 ifnull 相率会更好 select null = null返回也是null。null只能用is或is not

2023年8月6日 · 1 分钟

mongo操作

根据日期查询数量 db.getCollection("api_invocation_logs").find({"createdDate":{"$gte":ISODate("2024-01-09T00:00:00Z"),"$lt":ISODate("2024-01-10T00:00:00Z")}}).count() 查看某个字段不为空的数据 db.getCollection('waimao_company_info').find({'numberOfEmployees':{$ne:null}}) 查看某个表所有字段 mr = db.runCommand({ "mapreduce": "waimao_company_info", "map": function() { for (var key in this) { emit(key, null); } }, "reduce": function(key, stuff) { return null; }, "out": "waimao_company_info" + "_keys" }) db[mr.result].distinct("_id") 模糊查询 db.getCollection("rocket").find( {emails:/@/} )

2023年7月23日 · 1 分钟

es操作

ES建表 PUT http://121.46.197.112:9200/cty_test Content-Type: application/json { "settings": { "number_of_shards": 5, "number_of_replicas": 1, "analysis": { "analyzer": { "greek_lowercase_analyzer": { "filter": [ "lowercase" ], "type": "custom", "tokenizer": "standard" } } } }, "mappings": { "cty_test": { "properties": { "dupId": { "type": "text", "fields": { "keyword": { "ignore_above": 256, "type": "keyword" } } }, "id": { "type": "text", "fields": { "keyword": { "ignore_above": 256, "type": "keyword" } } }, "supplierId": { "type": "long" }, "addTime": { "format": "yyyy-MM-dd HH:mm:ss", "type": "date" }, "scCountry": { "type": "keyword" }, "shipName": { "type": "text" }, "manifestQuanityDouble": { "type": "float" }, "importerName": { "analyzer": "greek_lowercase_analyzer", "type": "text", "fields": { "keyword": { "ignore_above": 256, "type": "keyword" } } }, "tradeType": { "type": "integer" }, "arriveDate": { "format": "yyyy-MM-dd", "type": "date" }, "modeOfTransportation": { "type": "keyword" }, "modeoftransPortation": { "type": "text", "fields": { "keyword": { "ignore_above": 256, "type": "keyword" } } }, "weight": { "type": "double" } } } } } ES写入数据 POST http://121.46.197.112:9200/cty_test/cty_test Content-Type: application/json { "dupId": "34324", "id" : "545435", "supplierId" : 43636, "addTime" : "2021-02-05 08:08:08", "scCountry" : "rrr", "shipName" : "uuu", "manifestQuanityDouble" : 55.00, "importerName" : "ccd", "tradeType" : 43, "arriveDate" : "2018-09-03", "weight" : 33.0 } 查询某列不为空 ...

2023年7月9日 · 2 分钟