where子句的case不识别第二个case条件

ht4b089n  于 2021-06-15  发布在  Mysql
关注(0)|答案(2)|浏览(347)

下面的case子句我在一个内部连接中用作max value id上的where条件,case查询只根据第一个条件>lasteditor给出结果!“@example.com“不为空,则lasteditor!=”@example.com".
我想做的是,如果lasteditor!“@example.com“为空,则使用lasteditor=”@example.com“否则忽略@example.com 作为lasteditor并获取下一封lasteditor电子邮件

  1. WHERE
  2. CASE WHEN
  3. lasteditor != "abc@example.com" IS NOT NULL
  4. THEN
  5. lasteditor != "abc@example.com"
  6. WHEN
  7. lasteditor != "abc@example.com" IS NULL
  8. THEN
  9. lasteditor = "abc@example.com"
  10. ELSE NULL END

我要做的是,在最新的行上进行内部连接,其中lasteditor不是@example.com但是,如果lasteditor不存在,则根本没有结果@example.com,然后用@example.com

  1. INNER JOIN (SELECT MAX(id) AS maxid,dyna_id,LastEditor FROM t2
  2. WHERE
  3. CASE WHEN t2.lasteditor != "abc@example.com" IS NOT NULL
  4. THEN t2.lasteditor != "abc@example.com"
  5. WHEN t2.lasteditor != "abc@example.com" IS NULL
  6. THEN t2.lasteditor = "abc@example.com"
  7. ELSE NULL END
  8. GROUP BY t2.dyna_id) AS history ON main.id = history.dyna_id

总结

  1. WHERE t2.lasteditor = (CASE
  2. WHEN t2.lasteditor != 'abc@example.com' --- if rows exists --- THEN 'abc@example.com' (if rows exists after skipping rows where lasteditor is abc@example.com then use this case)
  3. WHEN t2.lasteditor != 'abc@example.com' --- if no rows exists --- THEN pick up the rows where 'abc@example.com' is the last editor
  4. END)

下面的代码做到了,我猜它做到了,这是一个巨大的表我正在工作,我希望它是显示正确的结果

  1. WHERE
  2. CASE
  3. WHEN
  4. lasteditor <> "abc@example.com" IS NOT NULL
  5. THEN
  6. lasteditor <> "abc@example.com"
  7. ELSE
  8. lasteditor <> "test"
  9. END
  10. GROUP BY
  11. hist.dyna_id

下面的完整查询

  1. SELECT
  2. "Total",
  3. COUNT(IF(main.case_status = "OPEN", 1, NULL)) AS "open_cases",
  4. COUNT(
  5. CASE
  6. WHEN
  7. owner.attr_role = "TM"
  8. AND main.tm_perception > "0 Star"
  9. AND main.tm_perception < "3 Star"
  10. THEN
  11. main.tm_perception
  12. WHEN
  13. owner.attr_role = "TL"
  14. AND main.tl_perception > "0 Star"
  15. AND main.tl_perception < "3 Star"
  16. THEN
  17. main.tl_perception
  18. WHEN
  19. owner.attr_role = "SE"
  20. AND main.owner_perception > "0 Star"
  21. AND main.owner_perception < "3 Star"
  22. THEN
  23. main.owner_perception
  24. WHEN
  25. owner.attr_role = "SE2"
  26. AND main.owner_perception > "0 Star"
  27. AND main.owner_perception < "3 Star"
  28. THEN
  29. main.owner_perception
  30. ELSE
  31. NULL
  32. END
  33. ) AS "bb", COUNT(
  34. CASE
  35. WHEN
  36. owner.attr_role = "TM"
  37. AND main.tm_perception > "2 Star"
  38. AND main.tm_perception < "5 Star"
  39. THEN
  40. main.tm_perception
  41. WHEN
  42. owner.attr_role = "TL"
  43. AND main.tl_perception > "2 Star"
  44. AND main.tl_perception < "5 Star"
  45. THEN
  46. main.tl_perception
  47. WHEN
  48. owner.attr_role = "SE"
  49. AND main.owner_perception > "2 Star"
  50. AND main.owner_perception < "5 Star"
  51. THEN
  52. main.owner_perception
  53. WHEN
  54. owner.attr_role = "SE2"
  55. AND main.owner_perception > "2 Star"
  56. AND main.owner_perception < "5 Star"
  57. THEN
  58. main.owner_perception
  59. ELSE
  60. NULL
  61. END
  62. ) AS "mb", COUNT(
  63. CASE
  64. WHEN
  65. owner.attr_role = "TM"
  66. AND main.tm_perception > "4 Star"
  67. AND main.tm_perception < "6 Star"
  68. THEN
  69. main.tm_perception
  70. WHEN
  71. owner.attr_role = "TL"
  72. AND main.tl_perception > "4 Star"
  73. AND main.tl_perception < "6 Star"
  74. THEN
  75. main.tl_perception
  76. WHEN
  77. owner.attr_role = "SE"
  78. AND main.owner_perception > "4 Star"
  79. AND main.owner_perception < "6 Star"
  80. THEN
  81. main.owner_perception
  82. WHEN
  83. owner.attr_role = "SE2"
  84. AND main.owner_perception > "4 Star"
  85. AND main.owner_perception < "6 Star"
  86. THEN
  87. main.owner_perception
  88. ELSE
  89. NULL
  90. END
  91. ) AS "tb", COUNT(
  92. CASE
  93. WHEN
  94. owner.attr_role = "na"
  95. THEN
  96. main.id
  97. ELSE
  98. NULL
  99. END
  100. ) AS "na"
  101. FROM
  102. gt AS main
  103. LEFT JOIN
  104. frontend_forms_users AS caseowner
  105. ON main.se_v_9 = caseowner.alias
  106. INNER JOIN
  107. (
  108. SELECT
  109. MAX(id) AS maxid,
  110. dyna_id AS dn
  111. FROM
  112. history_gt hist
  113. WHERE
  114. CASE
  115. WHEN
  116. lasteditor <> "abc@example.com" IS NOT NULL
  117. THEN
  118. lasteditor <> "abc@example.com"
  119. ELSE
  120. lasteditor <> "test"
  121. END
  122. GROUP BY
  123. hist.dyna_id
  124. )
  125. AS history
  126. ON main.id = dn
  127. LEFT JOIN
  128. history_gt AS owner2
  129. ON maxid = owner2.id
  130. LEFT JOIN
  131. frontend_forms_users AS owner
  132. ON owner2.LastEditor = owner.primary_email
  133. UNION ALL
  134. SELECT
  135. caseowner.attr_lob AS lob,
  136. COUNT(IF(main.case_status = "OPEN", 1, NULL)) AS "open_cases",
  137. COUNT(
  138. CASE
  139. WHEN
  140. owner.attr_role = "TM"
  141. AND main.tm_perception > "0 Star"
  142. AND main.tm_perception < "3 Star"
  143. THEN
  144. main.tm_perception
  145. WHEN
  146. owner.attr_role = "TL"
  147. AND main.tl_perception > "0 Star"
  148. AND main.tl_perception < "3 Star"
  149. THEN
  150. main.tl_perception
  151. WHEN
  152. owner.attr_role = "SE"
  153. AND main.owner_perception > "0 Star"
  154. AND main.owner_perception < "3 Star"
  155. THEN
  156. main.owner_perception
  157. WHEN
  158. owner.attr_role = "SE2"
  159. AND main.owner_perception > "0 Star"
  160. AND main.owner_perception < "3 Star"
  161. THEN
  162. main.owner_perception
  163. ELSE
  164. NULL
  165. END
  166. ) AS "bb", COUNT(
  167. CASE
  168. WHEN
  169. owner.attr_role = "TM"
  170. AND main.tm_perception > "2 Star"
  171. AND main.tm_perception < "5 Star"
  172. THEN
  173. main.tm_perception
  174. WHEN
  175. owner.attr_role = "TL"
  176. AND main.tl_perception > "2 Star"
  177. AND main.tl_perception < "5 Star"
  178. THEN
  179. main.tl_perception
  180. WHEN
  181. owner.attr_role = "SE"
  182. AND main.owner_perception > "2 Star"
  183. AND main.owner_perception < "5 Star"
  184. THEN
  185. main.owner_perception
  186. WHEN
  187. owner.attr_role = "SE2"
  188. AND main.owner_perception > "2 Star"
  189. AND main.owner_perception < "5 Star"
  190. THEN
  191. main.owner_perception
  192. ELSE
  193. NULL
  194. END
  195. ) AS "mb", COUNT(
  196. CASE
  197. WHEN
  198. owner.attr_role = "TM"
  199. AND main.tm_perception > "4 Star"
  200. AND main.tm_perception < "6 Star"
  201. THEN
  202. main.tm_perception
  203. WHEN
  204. owner.attr_role = "TL"
  205. AND main.tl_perception > "4 Star"
  206. AND main.tl_perception < "6 Star"
  207. THEN
  208. main.tl_perception
  209. WHEN
  210. owner.attr_role = "SE"
  211. AND main.owner_perception > "4 Star"
  212. AND main.owner_perception < "6 Star"
  213. THEN
  214. main.owner_perception
  215. WHEN
  216. owner.attr_role = "SE2"
  217. AND main.owner_perception > "4 Star"
  218. AND main.owner_perception < "6 Star"
  219. THEN
  220. main.owner_perception
  221. ELSE
  222. NULL
  223. END
  224. ) AS "tb", COUNT(
  225. CASE
  226. WHEN
  227. owner.attr_role = "na"
  228. THEN
  229. main.id
  230. ELSE
  231. NULL
  232. END
  233. ) AS "na"
  234. FROM
  235. gt AS main
  236. LEFT JOIN
  237. frontend_forms_users AS caseowner
  238. ON main.se_v_9 = caseowner.alias
  239. INNER JOIN
  240. (
  241. SELECT
  242. MAX(id) AS maxid,
  243. dyna_id AS dn
  244. FROM
  245. history_gt hist
  246. WHERE
  247. CASE
  248. WHEN
  249. lasteditor <> "abc@example.com" IS NOT NULL
  250. THEN
  251. lasteditor <> "abc@example.com"
  252. ELSE
  253. lasteditor <> "test"
  254. END
  255. GROUP BY
  256. hist.dyna_id
  257. )
  258. AS history
  259. ON main.id = dn
  260. LEFT JOIN
  261. history_gt AS owner2
  262. ON maxid = owner2.id
  263. LEFT JOIN
  264. frontend_forms_users AS owner
  265. ON owner2.LastEditor = owner.primary_email
  266. GROUP BY
  267. lob
  268. ORDER BY
  269. 2 DESC
egmofgnx

egmofgnx1#

你的问题,示例连接,子查询没有意义。。。话虽如此,让我试着这样重新问你的问题。如果我是对的,很好,如果不是,你的问题需要一些严肃的澄清。
这张table是“t2”。在它里面,对于任何给定的“dyna\u id”,您都有多个记录。它可以有一个或多个记录,并且每个“id”都是自动递增的。因此,对于给定的“dyna\u id”,您需要该键的最新记录。一旦您知道每个“dyna\u id”示例的最新“id”,您就想知道最后一个编辑记录的人是谁。对我来说,这意味着所有的记录都会有一个编辑文章的人,除非第一个记录是创建记录,并且有一个“addeditor”值,并且没有提供“lasteditor”值(但不要认为是这样)。
所以,要得到这个,它是一个由两部分组成的查询。首先,获取给定dyna\u id的最后一个id,然后获取与给定“id”匹配的最后一个编辑器的名称。最后,把这个答案和你的另一个加入main。。。

  1. INNER JOIN
  2. -- getting the Details for that last edited per Dyna_ID
  3. (select
  4. T2.Dyna_id,
  5. T2.ID,
  6. coalesce( T2.LastEditor, 'abc@example.com' ) as LastEditorPerDynaID
  7. from
  8. T2
  9. -- getting the last ID for any given Dyna_ID
  10. JOIN ( select
  11. tmp1.dyna_id,
  12. max(tmp1.id) as MaxIdPerDynaID
  13. from
  14. t2 tmp1
  15. group by
  16. tmp1.dyna_id ) PQ
  17. on T2.Dyna_ID = PQ.Dyna_ID
  18. AND T2.ID = PQ.MaxIdPerDynaID ) history
  19. ON main.id = history.dyna_id

澄清@example.com
您的要求@example.com. 下面是“t2”表的一些示例数据。

  1. ID Dyna_ID LastEditor
  2. 1 A ME
  3. 2 B abc@example.com
  4. 3 A HER
  5. 4 C ME
  6. 5 B HIM
  7. 6 A abc@example.com
  8. 7 A HIM
  9. 8 C HER
  10. 9 B abc@example.com
  11. 10 D abc@example.com

从上面的数据中,我得到了唯一的dyna\u id值a,b和c,d。因此,来自原始查询的max(id)和最终对应的最后一个编辑器将导致

  1. Dyna_ID HighestID LastEditor
  2. A 7 HIM
  3. B 9 abc@example.com
  4. C 8 HER
  5. D 10 abc@example.com

因此,如果您只查找最后一个编辑器不是@example.com,则希望“b”dynaid拉取“him”的id=5。但对于“d”王朝,他们唯一的记录是@lexample.com“,他们没有其他的,所以保留那个是可以的。
如果是这样,那么我的内部pq(预查询)将分别基于“”条目的任一条件获得最大id。因为我们总是会有至少一个条目,但要有或没有各自的最大id。。。

  1. INNER JOIN
  2. -- getting the Details for that last edited per Dyna_ID
  3. (select
  4. T2.Dyna_id,
  5. T2.ID,
  6. coalesce( T2.LastEditor, 'abc@example.com' ) as LastEditorPerDynaID
  7. from
  8. T2
  9. -- getting the last ID for any given Dyna_ID
  10. JOIN
  11. ( select
  12. PQ1.Dyna_ID,
  13. case when PQ1.MaxIdNotAbcEditor > 0
  14. then PQ1.MaxIdNotAbcEditor
  15. else PQ1.MaxIdAsAbcEditor end as FinalJoinID
  16. from
  17. ( select
  18. tmp1.dyna_id,
  19. -- get max ID if NOT the 'abc' editor
  20. max( case when NOT tmp1.LastEditor = 'abc@example.com' then tmp1.id else 0 end ) as MaxIdNotAbcEditor,
  21. -- get max ID if IT IS the 'abc' editor
  22. max( case when tmp1.LastEditor = 'abc@example.com' then tmp1.id else 0 end ) as MaxIdAsAbcEditor
  23. from
  24. t2 tmp1
  25. group by
  26. tmp1.dyna_id ) PQ1 ) PQ
  27. on T2.Dyna_ID = PQ.Dyna_ID
  28. AND T2.ID = PQ.FinalJoinID ) history
  29. ON main.id = history.dyna_id

基于第二个查询,它由三部分组成。最内部的“pq1”预查询1,对于任何给定的dyna\u id,我都将根据各自的部分获取最后一个id

  1. Dyna_ID MaxIdNotAbcEditor MaxIdAsAbcEditor
  2. A 7 6
  3. B 5 9
  4. C 8 0
  5. D 0 10

可以看到,dynaid“c”没有“@”记录,所以它的最大id为 editor=0。。。与dynaid“d”相反。它只有一个“@”,所以非编辑器为0,编辑器为10。。跟我走这么远?这是正确的假设吗?
好 啊。所以现在,我们对任何一个dyna id都有一个最大可能的分类。因此,现在,在创建仅由dyna id和max id组成的外部“pq”结果时,这将与case/一起结束。如果“i'm not the editor”列有一个值,请使用该值作为id。否则,获取唯一的其他选项。。。编辑器的条目。结果如下:

  1. Dyna_ID FinalJoinID
  2. A 7
  3. B 5
  4. C 8
  5. D 10
展开查看全部
csbfibhn

csbfibhn2#

下面是一个可能有用的场景。主表是与作者的文章和对任何文章可以由作者或任何其他人评论。我只想显示不是作者的任何人的最新评论,除非只有作者发表了评论。
编辑
显示非作者的任何人的最新评论-除非只有作者评论,然后获取最新的作者评论。

  1. CREATE TABLE article(
  2. id INTEGER NOT NULL PRIMARY KEY
  3. ,author VARCHAR(100) NOT NULL
  4. ,title VARCHAR(100) NOT NULL
  5. );
  1. INSERT INTO article(id,author,title)
  2. VALUES
  3. (1,'abc@example.com','a first article')
  4. , (2,'def@example.com','beware of between')
  5. , (3,'ghi@example.com','to be or not whatever');
  1. CREATE TABLE comments(
  2. id INTEGER NOT NULL PRIMARY KEY
  3. ,article_id INTEGER NOT NULL
  4. ,author VARCHAR(100) NOT NULL
  5. ,body VARCHAR(200) NOT NULL
  6. );
  1. INSERT INTO comments(id,article_id,author,body)
  2. VALUES
  3. (1,1,'abc@example.com','first comment')
  4. , (2,1,'def@example.com','second comment')
  5. , (3,1,'ghi@example.com','third comment')
  6. , (4,1,'abc@example.com','2nd author comment')
  7. , (5,3,'ghi@example.com','only comment')
  8. ;

相关查询方法(任何mysql版本)通过 order by case when c.author <> t.author then 1 else 2 end, id DESC 以及 limit ```
select
a.*
, c.*
from (
select
t.*
, (select id
from comments as c
where c.article_id = t.id
order by
case when c.author <> t.author then 1 else 2 end
, id DESC
limit 1) AS max_comment_id
from article as t
) a
left join comments as c on a.max_comment_id = c.id
;

id | author | title | max_comment_id | id | article_id | author | body
-: | :-------------- | :-------------------- | -------------: | ---: | ---------: | :-------------- | :------------
1 | abc@example.com | a first article | 3 | 3 | 1 | ghi@example.com | third comment
2 | def@example.com | beware of between | null | null | null | null | null
3 | ghi@example.com | to be or not whatever | 5 | 5 | 3 | ghi@example.com | only comment

  1. 分组子查询方法(任何mysql版本)

select
a.*
, c.*
from article a
left join (
select
c.article_id
, max(coalesce((case when c.author <> a.author then c.id end), (case when c.author = a.author then c.id end))) max_comment_id
from comments as c
inner join article a on c.article_id = a.id
group by
c.article_id
) d on a.id = d.article_id
left join comments c on d.max_comment_id = c.id
;

id | author | title | id | article_id | author | body
-: | :-------------- | :-------------------- | ---: | ---------: | :-------------- | :-----------------
1 | abc@example.com | a first article | 4 | 1 | abc@example.com | 2nd author comment
2 | def@example.com | beware of between | null | null | null | null
3 | ghi@example.com | to be or not whatever | 5 | 3 | ghi@example.com | only comment

  1. 行数()方法的派生表(mysql 8以后)

select
a.*
, c.*
from article a
left join (
select
c.*
, row_number() over(partition by article_id
order by case when c.author <> a.author then 1 else 2 end, id DESC
) as rn
from comments as c
inner join article a on c.article_id = a.id
) c on a.id = c.article_id and c.rn = 1

id | author | title | id | article_id | author | body | rn
-: | :-------------- | :-------------------- | ---: | ---------: | :-------------- | :------------ | ---:
1 | abc@example.com | a first article | 3 | 1 | ghi@example.com | third comment | 1
2 | def@example.com | beware of between | null | null | null | null | null
3 | ghi@example.com | to be or not whatever | 5 | 3 | ghi@example.com | only comment | 1

  1. 公共表表达式和行数()方法(mysql 8以后)

with max_comments as (
select
c.*
, row_number() over(partition by article_id
order by case when c.author <> a.author then 1 else 2 end, id DESC
) as rn
from comments as c
inner join article a on c.article_id = a.id
)
select
a.*
, c.*
from article a
left join max_comments c on a.id = c.article_id and c.rn = 1

id | author | title | id | article_id | author | body | rn
-: | :-------------- | :-------------------- | ---: | ---------: | :-------------- | :------------ | ---:
1 | abc@example.com | a first article | 3 | 1 | ghi@example.com | third comment | 1
2 | def@example.com | beware of between | null | null | null | null | null
3 | ghi@example.com | to be or not whatever | 5 | 3 | ghi@example.com | only comment | 1

  1. db<>在这里摆弄
展开查看全部

相关问题