【问题标题】:How to use select with a variable如何将 select 与变量一起使用
【发布时间】:2018-04-05 14:54:27
【问题描述】:

为了简化我有一个表,我只想用蓝色的数据进行选择,这对我来说很棘手,因为我必须使用 where textanswer='dimibilli d2' 但我只会得到记录 5、7、10、12 但是我想把 6、8、9 弄到

我该怎么做?

【问题讨论】:

  • 你用的是什么数据库,SQL只是一种查询语言
  • "但是我想把 6,8,9 弄到" 为什么?因为他们的TextAnswer 是空白的?或者是什么?请解释为什么你想要你想要的结果。无论如何,我在这里看不到任何变量...
  • @underscore_d;因为他们有相同的transdocnumber。一些 SO 帖子就像谜题:D
  • @underscore_d 我猜 OP 还想在 where 子句中包含空字符串行
  • 谢谢,我举个例子,例如,我想选择与 Dimobilli D2 相关的所有行,因为记录 5 很容易,但我也想要记录 6 ,但是where子句上的选择不能相同,现在mill必须是transdocnunber 5055,我想我必须使用变量并使用循环,我必须找到dimobilli d2然后查看transdocnumber并使用它来代替dimobilli d2

标签: sql where-in


【解决方案1】:

选择具有所需文本列的行并匹配相应的 Id 以获得所需的输出。

declare @table table (transdocnumber int, textanswer varchar(20))
insert @table (transdocnumber,textanswer)
select 4631, 'Dimibilli D4' union all
select 4631, '' union all
select 5055, 'Dimibilli D2' union all
select 5055, '' union all
select 5270, 'Dimibilli D2' union all 
select 5270, '' union all 
select 5270, '' union all 
select 5513, 'Dimibilli D2' union all 
select 6279, 'Dimibilli D4' union all 
select 6616, 'Dimibilli D2' union all 
select 6773, 'Dimibilli D4' 

select t.transdocnumber,t.textanswer from (
select transdocnumber from @table where textanswer = 'Dimibilli D2' ) x
inner join @table t on x.transdocnumber = t.transdocnumber where t.textanswer = ''
union all
select transdocnumber,textanswer from @table where textanswer = 'Dimibilli D2'
order by transdocnumber

解决方案:

SELECT t.transdocnumber, t.extrafield_id, t.textanswer, t.BooleanAnswer, t.DtAlt
FROM (
    SELECT *
    FROM TABLE
    WHERE textanswer = 'Dimibilli D2'
    ) x
INNER JOIN TABLE t ON x.transdocnumber = t.transdocnumber
WHERE t.textanswer = ''

UNION ALL

SELECT transdocnumber, extrafield_id, textanswer, BooleanAnswer, DtAlt
FROM TABLE
WHERE textanswer = 'Dimibilli D2'
ORDER BY transdocnumber

【讨论】:

  • 但是记录号4也会被选中,不起作用
  • 你说得对,只有6,8和9的逻辑是什么
  • 是我的错误,transdocnumber 列和 textanswer 之间存在关系,我想查看在 dimobbili d2 的 textanswer 上具有的所有 transdocnumber
  • @AntónioSilva 编辑了答案以满足您所需的输出
  • 它就像一个魅力 :-) 只需添加这个“WHERE t.textanswer = 'Dimobilli D2' or t.textanswer = ''” 并给我正确的输出。非常感谢:-)
【解决方案2】:

你似乎想要:

select t.*
from t
where t.tansdocnumber in (select t2.transdocnumber from t t2 where t2.textanswer = 'Dimobili D2);

【讨论】:

    【解决方案3】:
    select * from table
    where textanswer not in ('dimobilli d4')
    

    【讨论】:

    • 行不通,您只看到或多或少的 10 行,但接近 6000 条记录
    • 请添加一些示例测试数据和逻辑
    【解决方案4】:

    据我所知,您希望返回带有 textanswer='dimibilli d2' 和 textanswer='dimibilli d2' 的行,而不是第 4 行。

    你不能这样做,你应该以某种方式区分你想要的行。

    使用

    select * from table where (textanswer='dimibilli d2' or textanswer = '')

    正如 Ven 建议的那样,您将得到第 4,5,6,7,8,9,10,12 行,但您似乎只想要 5,6,7,8,9,10,12

    例如,您可以使用:

    select * from table where (textanswer='dimibilli d2' or textanswer = '') and TransDocNumber>5000;

    这应该可以工作

    【讨论】:

    • Op 不想在 filter 子句中硬编码,他想使用 transdocnumber 列,请参考我上面编辑的解决方案,它应该可以工作
    【解决方案5】:

    尝试使用以下查询:

    select * from table
    where (TextAnswer='Dimobili D2' or BooleanAnswer = '1') and TextAnswer!=''
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-29
      • 2022-01-12
      • 2021-01-09
      相关资源
      最近更新 更多