【问题标题】:select at least one field have the value in another table选择至少一个字段具有另一个表中的值
【发布时间】:2020-09-09 22:00:37
【问题描述】:

我有一个带有字段 meeting_start_date、call_start_date 的表格报告和一个我想与另一个带有字段值的表格 dcalendar 连接的表格。现在可以从我的查询中选择类型

select
    a.id,a.meeting_start_date,a.call_start_date
from
  report as a
left join
  dcalendar b
    on b.datevalue=a.meeting_start_date
left join
  dcalendar c
    on c.datevalue=a.call_start_date
where
  (a.lead_stage_c='test')
  and
  (b.datevalue > '2020-07-31' and b.datevalue < '2020-08-05')
  or
  ( c.datevalue > '2020-07-31' and c.datevalue < '2020-08-05')
  and
  type='test'

报告表

dcalender
id datevalue
1  "2020-08-01 00:00:00"
2  "2020-08-02 00:00:00"
3  "2020-08-03 00:00:00"
4  "2020-08-04 00:00:00
5  "2020-08-05 00:00:00"

我想在报告表中选择 meeting_start_date 或 call_start_date 中 dcalender 表 dvalue 大于“2020-08-01”且小于或等于“2020-08-04”的行。

在我的示例中,我需要选择 1,2,3,4,5 试过了

我在查询中做错了什么

【问题讨论】:

  • 将长查询布置为单行几乎无法阅读,因此我为您重新格式化。随意调整样式,但请不要发布单行查询。至于猜测,您在 where 子句中的括号是错误的。 Probaby想要在OR的两个部分周围增加()

标签: mysql sql


【解决方案1】:

您的括号已关闭:

where a.lead_stage_c = 'Target-MQL' and
      type = 'test' and
      (b.datevalue > '2020-07-31' and b.datevalue < '2020-08-05' or
       c.datevalue > '2020-07-31' and c.datevalue < '2020-08-05'
      )

【讨论】:

  • 即使没有得到正确的输出,测试中也会出现拼写错误
  • @user3386779 - 编辑您的问题以包含此处建议的更改,并显示您获得的输出与您实际想要的输出的比较。遗憾的是,诸如“没有得到正确的输出”之类的短语没有帮助,我们需要更多信息......
猜你喜欢
  • 2017-04-11
  • 1970-01-01
  • 2013-10-04
  • 2020-07-29
  • 1970-01-01
  • 1970-01-01
  • 2021-05-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多