pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

HAVING vs WHERE vs GROUP BY 子句,何時使用它們以及是

HAVING vs WHERE vs GROUP BY clauses, when to use them and if you use amp;#39; amp;#39;(HAVING vs WHERE vs GROUP BY 子句,何時使用它們以及是否使用 amp;#39;amp;#39;)
本文介紹了HAVING vs WHERE vs GROUP BY 子句,何時使用它們以及是否使用 ''的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

希望這篇文章能幫助我和許多像我一樣的人更好地理解 WHERE、HAVING、GROUP BY 等問題.每個人都有自己的語法處理方式,因為在 MYSQL 中有不止一種方法可以使某些事情起作用我的想法是幫助我完成這項工作,同時也幫助整個社區:) 下面是設計我的查詢的一種建議方法.

SELECT t1.post_id, t2.name,MAX(case when meta_key = 'value' THEN `meta_value` ELSE NULL END) 作為電子郵件,MAX(CASE WHEN `meta_key` = 'value' THEN `meta_value` ELSE NULL END) 作為 CustomerId,MAX(CASE WHEN `meta_key` = 'value' THEN `meta_value` ELSE NULL END) 作為 DeliveryDate,MAX(CASE WHEN `meta_key` = 'value' THEN `meta_value` ELSE NULL END) 作為 DeliveryTime,MAX(CASE WHEN `meta_key` = 'value' THEN `meta_value` ELSE NULL END) 作為 DeliveryType,MAX(case when meta_key = 'value' THEN `meta_value` ELSE NULL END) 作為 Zip,MAX(case when meta_key = 'value' THEN `meta_value` ELSE NULL END) 作為 OrderNote,MAX(case when meta_key = 'value' THEN `meta_value` ELSE NULL END) 作為 PaymentTotal,MAX(case when meta_key = 'value' THEN `meta_value` ELSE NULL END) 作為 OrderStatusFROM table_A t1find_in_set(t1.post_id, t2.payment_ids) 上的內連接 table_B t2其中 OrderStatus rlike '%trans%|ready'和 DeliveryDate >= current_date - 間隔 7 天和 DeliveryType = 'pickup'通過...分組t1.post_id,t2.name

這會產生一個錯誤 >>>>#1054 - 'where 子句'中的未知列'DeliveryDate'"我認為它會產生這個錯誤,因為orderStatus"不是實際的列名,而是從另一列中提取的值,然后通過 :

MAX(case when meta_key = '_order_status' THEN `meta_value` ELSE NULL END) as OrderStatus

所以我假設我需要在語句的 SELECT 區域和 WHERE 區域中將名稱括在 ' ' 中.但是產生錯誤 >>>>>>>>>>>警告:#1292 截斷的錯誤日期值:'DeliveryDate'"

為什么會這樣?解決方案是什么?

<塊引用><塊引用><塊引用>

EDITING 因為有人建議不能以上述方式使用 WHERE 子句,所以我使用下面的代碼使用了 HAVING 子句.代碼如下:

選擇.......^^從上面......

FROM table_A t1find_in_set(t1.post_id, t2.payment_ids) 上的內連接 table_B t2GROUP BY post_idHAVING DeliveryDate = (DATE_SUB(CURDATE(), INTERVAL 7 DAY))AND DeliveryType = '提貨'AND OrderStatus = '準備好'或 OrderStatus = 'transit'ORDER BY 'DeliveryTime' DESC

以上也行不通.這里的問題是 AND 子句更重要,并且似乎淘汰了日期過濾器.當我使用此代碼時,無論日期如何,它都會返回所有記錄.

編輯 2 >>>>>>>>>也試過了,但它仍然沒有過濾掉 3 個月大的條目

選擇.......^^從上面......

FROM table_A t1find_in_set(t1.post_id, t2.payment_ids) 上的內連接 table_B t2GROUP BY post_idHAVING MAX(CASE WHEN 'meta_key' = 'value' THEN 'meta_value' ELSE NULL END)>= current_date - 間隔 7 天AND DeliveryType = '提貨'AND OrderStatus = '準備好'或 OrderStatus = 'transit'ORDER BY 'DeliveryTime' DESC

編輯 3 >>>>>>>>簡化代碼.結果一樣.即使使用 CURDATE() 仍然顯示 3 個月前的記錄

......................

FROM table_A t1find_in_set(t1.post_id, t2.payment_ids) 上的內連接 table_B t2GROUP BY post_idHAVING MAX(CASE WHEN 'meta_key' = 'value' THEN 'meta_value' ELSE NULL END)= CURDATE()AND DeliveryType = '提貨'AND OrderStatus = '準備好'或 OrderStatus = 'transit'ORDER BY 'DeliveryTime' DESC

編輯 4 >>>>>>>>>>>>>>>>>最小的例子...

選擇 t1.post_id, t2.name,

 MAX(CASE WHEN `meta_key` = 'value' THEN `meta_value` ELSE NULL END) 作為 DeliveryDate,MAX(CASE WHEN `meta_key` = 'value' THEN `meta_value` ELSE NULL END) 作為 DeliveryTime,MAX(CASE WHEN `meta_key` = 'value' THEN `meta_value` ELSE NULL END) 作為 DeliveryType,MAX(case when meta_key = 'value' THEN `meta_value` ELSE NULL END) 作為 OrderStatusFROM table_A t1find_in_set(t1.post_id, t2.payment_ids) 上的內連接 table_B t2GROUP BY post_idHAVING MAX(CASE WHEN 'meta_key' = 'value' THEN 'meta_value' ELSE NULL END)= CURDATE()AND DeliveryType = '提貨'AND OrderStatus = '準備好'或 OrderStatus = 'transit'ORDER BY 'DeliveryTime' DESC

我希望這只會返回今天的記錄.IT 是在滿足其他 HAVING 子句要求的同時返回所有時間的所有記錄

解決方案

按照@O 的答案.Jones 是一個嵌套查詢:

SELECT post_id, 姓名, 電子郵件, 顧客ID, 郵寄日期, 交貨時間, 交貨類型, 壓縮, 訂單備注, 支付總額, 訂單狀態從(選擇 t1.post_id, t2.name, MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) 作為電子郵件, MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) 作為 CustomerId, MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) 作為 DeliveryDate, MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) 作為 DeliveryTime, MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as DeliveryType, MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as Zip, MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as OrderNote, MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as PaymentTotal, MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) 作為 OrderStatusFROM table_A t1內JOIN 表_B t2ON FIND_IN_SET(t1.post_id, t2.payment_ids)團體由 t1.post_id, t2.name) AS 派生表WHERE OrderStatus RLIKE '%trans%|ready'AND DeliveryDate >= CURRENT_DATE - 間隔 7 天AND DeliveryType = '提貨'

Hopefully this post will help me and many others like me better understand the issues of WHERE, HAVING, GROUP BY etc. Everyone has their own way of doing syntax and since there is more than one way to make something work in MYSQL the idea would be to help me make this work while helping the community at large too :) Below is one suggested way of designing my query.

SELECT t1.post_id, t2.name,
           MAX(case when meta_key = 'value' THEN `meta_value` ELSE NULL END) as  Email,
       MAX(CASE WHEN `meta_key` = 'value' THEN `meta_value` ELSE NULL END) as CustomerId,
       MAX(CASE WHEN `meta_key` = 'value' THEN `meta_value` ELSE NULL  END) as DeliveryDate,
        MAX(CASE WHEN `meta_key` = 'value' THEN `meta_value` ELSE NULL  END) as DeliveryTime,
        MAX(CASE WHEN `meta_key` = 'value' THEN `meta_value` ELSE NULL  END) as DeliveryType,
          MAX(case when meta_key = 'value' THEN `meta_value` ELSE NULL END) as  Zip,
       MAX(case when meta_key = 'value' THEN `meta_value` ELSE NULL END) as  OrderNote,
       MAX(case when meta_key = 'value' THEN `meta_value` ELSE NULL END) as  PaymentTotal,
       MAX(case when meta_key = 'value' THEN `meta_value` ELSE NULL END) as  OrderStatus

FROM table_A  t1
inner join table_B t2 on find_in_set(t1.post_id, t2.payment_ids)
where  OrderStatus rlike '%trans%|ready'
    and DeliveryDate >= current_date - interval 7 day
    and DeliveryType = 'pickup'
group by 
    t1.post_id, 
    t2.name

This produces an error >>>> "#1054 - Unknown column 'DeliveryDate' in 'where clause'" I presume it produces this error since "orderStatus" is not an actual column name but is a value being pulled from another column and then being made its own column through the :

MAX(case when meta_key = '_order_status' THEN `meta_value` ELSE NULL END) as  OrderStatus

So I presumed that I needed to enclose the name in ' ' both in the SELECT area of the statements and in the WHERE area. BUT that produces the error >>>>>>>>>>>> "Warning: #1292 Truncated incorrect date value: 'DeliveryDate'"

Why would this be and whats the solution?

EDITING Because some have suggested the WHERE clause can not be used in the manner above, I have used the HAVING clause using the code below. Here is the code:

SELECT.......^^from above..............

FROM table_A t1
inner join table_B t2 on find_in_set(t1.post_id, t2.payment_ids)
GROUP BY post_id
HAVING DeliveryDate = (DATE_SUB(CURDATE(), INTERVAL 7 DAY)) 
AND DeliveryType = 'pickup' 
AND  OrderStatus = 'ready' 
OR OrderStatus = 'transit'
ORDER BY 'DeliveryTime'  DESC

The above doesnt work either. The issue here is that the AND clauses are more important and seem to kncok out the date filter. When I use this code, this returns all records regardless of dates.

EDIT 2 >>>>>>>>>> Tried this too btu it still doesnt filter out the 3 month old entry

SELECT.......^^from above..............

FROM table_A t1
inner join table_B t2 on find_in_set(t1.post_id, t2.payment_ids)
GROUP BY post_id
HAVING MAX(CASE WHEN 'meta_key' = 'value' THEN 'meta_value' ELSE NULL END)>= current_date - interval 7 day 
AND DeliveryType = 'pickup' 
AND  OrderStatus = 'ready' 
OR OrderStatus = 'transit'
ORDER BY 'DeliveryTime'  DESC

EDIT 3 >>>>>>>>>> Simplifying the code. Same result. Even with CURDATE() still shows 3 month old records

......................

FROM table_A t1
inner join table_B t2 on find_in_set(t1.post_id, t2.payment_ids)
GROUP BY post_id
HAVING MAX(CASE WHEN 'meta_key' = 'value' THEN 'meta_value' ELSE NULL END)= CURDATE()
AND DeliveryType = 'pickup' 
AND  OrderStatus = 'ready' 
OR OrderStatus = 'transit'
ORDER BY 'DeliveryTime'  DESC

EDIT 4 >>>>>>>>>>>>>>>>>>>>>> minimal exmaple...

SELECT t1.post_id, t2.name,

   MAX(CASE WHEN `meta_key` = 'value' THEN `meta_value` ELSE NULL  END) as DeliveryDate,
    MAX(CASE WHEN `meta_key` = 'value' THEN `meta_value` ELSE NULL  END) as DeliveryTime,
    MAX(CASE WHEN `meta_key` = 'value' THEN `meta_value` ELSE NULL  END) as DeliveryType,
   MAX(case when meta_key = 'value' THEN `meta_value` ELSE NULL END) as  OrderStatus

FROM table_A t1
inner join table_B t2 on find_in_set(t1.post_id, t2.payment_ids)
GROUP BY post_id
HAVING MAX(CASE WHEN 'meta_key' = 'value' THEN 'meta_value' ELSE NULL END)= CURDATE()
AND DeliveryType = 'pickup' 
AND  OrderStatus = 'ready' 
OR OrderStatus = 'transit'
ORDER BY 'DeliveryTime'  DESC

I expect this to return the records only of today. IT is return all records of all time while meeting the other HAVING clause requirements

解決方案

The answer as per @O. Jones is a nested query:

SELECT post_id
     , name
     , Email
     , CustomerId
     , DeliveryDate
     , DeliveryTime
     , DeliveryType
     , Zip
     , OrderNote
     , PaymentTotal
     , OrderStatus
  FROM ( SELECT t1.post_id
              , t2.name
              , MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as Email
              , MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as CustomerId
              , MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as DeliveryDate
              , MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as DeliveryTime
              , MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as DeliveryType
              , MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as Zip
              , MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as OrderNote
              , MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as PaymentTotal
              , MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as OrderStatus
           FROM table_A t1
         INNER 
           JOIN table_B t2 
             ON FIND_IN_SET(t1.post_id, t2.payment_ids)  
         GROUP 
             BY t1.post_id
              , t2.name  
       ) AS derived_table
 WHERE OrderStatus RLIKE '%trans%|ready'
   AND DeliveryDate >= CURRENT_DATE - INTERVAL 7 DAY
   AND DeliveryType = 'pickup'

這篇關于HAVING vs WHERE vs GROUP BY 子句,何時使用它們以及是否使用 &amp;#39;&amp;#39;的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Typeorm Does not return all data(Typeorm 不返回所有數據)
MySQL return extra records when using a long type number to filter varchar type(MySQL在使用長類型數字過濾varchar類型時返回額外記錄)
MySQL Error #1071 - Specified key was too long; max key length is 767 bytes(MySQL 錯誤 #1071 - 指定的鍵太長;最大密鑰長度為 767 字節)
MySQL command-line table column width with utf8(MySQL命令行表列寬與utf8)
Python unicode encoding issue(Python unicode 編碼問題)
Create a MySQL stored function with a dynamic number of arguments(創建一個帶有動態參數數量的 MySQL 存儲函數)
主站蜘蛛池模板: 硫酸钡厂家_高光沉淀硫酸钡价格-河南钡丰化工有限公司 | 氟塑料磁力泵-不锈钢离心泵-耐腐蚀化工泵厂家「皖金泵阀」 | 衡阳耐适防护科技有限公司——威仕盾焊接防护用品官网/焊工手套/焊接防护服/皮革防护手套 | 六维力传感器_六分量力传感器_模腔压力传感器-南京数智微传感科技有限公司 | 珠海冷却塔降噪维修_冷却塔改造报价_凉水塔风机维修厂家- 广东康明节能空调有限公司 | 合肥抖音SEO网站优化-网站建设-网络推广营销公司-百度爱采购-安徽企匠科技 | 干式磁选机_湿式磁选机_粉体除铁器-潍坊国铭矿山设备有限公司 | 空气能暖气片,暖气片厂家,山东暖气片,临沂暖气片-临沂永超暖通设备有限公司 | 高温链条油|高温润滑脂|轴承润滑脂|机器人保养用油|干膜润滑剂-东莞卓越化学 | 海外仓系统|国际货代系统|退货换标系统|WMS仓储系统|海豚云 | 南京泽朗生物科技有限公司 | 青海电动密集架_智能密集架_密集架价格-盛隆柜业青海档案密集架厂家 | 组织研磨机-高通量组织研磨仪-实验室多样品组织研磨机-东方天净 传递窗_超净|洁净工作台_高效过滤器-传递窗厂家广州梓净公司 | 鹤壁创新仪器公司-全自动量热仪,定硫仪,煤炭测硫仪,灰熔点测定仪,快速自动测氢仪,工业分析仪,煤质化验仪器 | COD分析仪|氨氮分析仪|总磷分析仪|总氮分析仪-圣湖Greatlake | 铸铝门厂家,别墅大门庭院大门,别墅铸铝门铜门[十大品牌厂家]军强门业 | 翰香原枣子坊加盟费多少钱-正宗枣核糕配方培训利润高飘香 | 【灵硕展览集团】展台展会设计_展览会展台搭建_展览展示设计一站式服务公司 | 进口试验机价格-进口生物材料试验机-西安卡夫曼测控技术有限公司 | 承插管件_不锈钢承插管件_锻钢高压管件-温州科正阀门管件有限公司 | 食品机械专用传感器-落料放大器-低价接近开关-菲德自控技术(天津)有限公司 | 干式变压器厂_干式变压器厂家_scb11/scb13/scb10/scb14/scb18干式变压器生产厂家-山东科锐变压器有限公司 | 招商帮-一站式网络营销服务|搜索营销推广|信息流推广|短视视频营销推广|互联网整合营销|网络推广代运营|招商帮企业招商好帮手 | 新能源汽车教学设备厂家报价[汽车教学设备运营18年]-恒信教具 | 打包钢带,铁皮打包带,烤蓝打包带-高密市金和金属制品厂 | 手术室净化装修-手术室净化工程公司-华锐手术室净化厂家 | 避光流动池-带盖荧光比色皿-生化流动比色皿-宜兴市晶科光学仪器 东莞爱加真空科技有限公司-进口真空镀膜机|真空镀膜设备|Polycold维修厂家 | 标准光源箱|对色灯箱|色差仪|光泽度仪|涂层测厚仪_HRC大品牌生产厂家 | 【ph计】|在线ph计|工业ph计|ph计厂家|ph计价格|酸度计生产厂家_武汉吉尔德科技有限公司 | 剪刃_纵剪机刀片_分条机刀片-南京雷德机械有限公司 | 加盟店-品牌招商加盟-创业项目商机平台 | 合肥抖音SEO网站优化-网站建设-网络推广营销公司-百度爱采购-安徽企匠科技 | 塑料瓶罐_食品塑料瓶_保健品塑料瓶_调味品塑料瓶–东莞市富慷塑料制品有限公司 | 广东护栏厂家-广州护栏网厂家-广东省安麦斯交通设施有限公司 | 振动台-振动试验台-振动冲击台-广东剑乔试验设备有限公司 | 【甲方装饰】合肥工装公司-合肥装修设计公司,专业从事安徽办公室、店面、售楼部、餐饮店、厂房装修设计服务 | 超细|超微气流粉碎机|气流磨|气流分级机|粉体改性机|磨粉机|粉碎设备-山东埃尔派粉体科技 | 欧美日韩国产一区二区三区不_久久久久国产精品无码不卡_亚洲欧洲美洲无码精品AV_精品一区美女视频_日韩黄色性爱一级视频_日本五十路人妻斩_国产99视频免费精品是看4_亚洲中文字幕无码一二三四区_国产小萍萍挤奶喷奶水_亚洲另类精品无码在线一区 | 蒸汽热收缩机_蒸汽发生器_塑封机_包膜机_封切收缩机_热收缩包装机_真空机_全自动打包机_捆扎机_封箱机-东莞市中堡智能科技有限公司 | 合肥仿石砖_合肥pc砖厂家_合肥PC仿石砖_安徽旭坤建材有限公司 | 国资灵活用工平台_全国灵活用工平台前十名-灵活用工结算小帮手 |