【问题标题】:SQL: How to replace one with two?SQL:如何用两个替换一个?
【发布时间】:2011-08-02 19:31:30
【问题描述】:

我有一个“第一”表:

我正在尝试编写查询。

    If first.type = 'MM'

    then replace this line in two new lines

    where first.type = 'HH' and first.type = 'VV'.

例如:first.type = 'MM'的那一行:

我想这样替换这一行:

结果:

有人知道如何做到这一点吗?

我正在使用 MS SQL Server Management Studio Express。

【问题讨论】:

    标签: sql sql-server-2005 replace line


    【解决方案1】:
    select id,freq,parity,line,type,gain,obdim from [first] where type&lt&gt'MM' union select id,freq,parity,line,'HH' as type,gain,obdim from [first] where type='MM' union select id,freq,parity,line,'VV' as type,gain,obdim from [first] where type='MM'

    【讨论】:

    • +1 但请将 * 扩展为实际列。这张表一更新,一切就崩溃了。
    【解决方案2】:
    select id,frequency, 'VV' type --, .. 
    from table1
    where type='MM'
    union all
    select id,frequency, 'HH' as type --, .. 
    from table1
    where type='MM'
    

    【讨论】:

      【解决方案3】:

      这个解决方案可能非常具体,因为它暗示type 列只能有值HHMMVV

      SELECT
        f.ID,
        f.freq,
        f.parity,
        f.line,
        t.type,
        f.gain,
        f.obdim
      FROM [first] f
        INNER JOIN (
          SELECT 'HH' AS type
          UNION ALL
          SELECT 'VV'
        ) t ON f.type IN (t.type, 'MM')
      

      【讨论】:

      • @Juronis:不知道为什么会有这样的错误。不过我稍微改了一下,现在应该没问题了。还要感谢您的评论,我注意到我的解决方案中有一个错误。也修复了它。
      • 嗯,还是不行。我收到错误“查询输入必须包含至少一个表或查询”..
      • 您在哪个 RDBMS 中尝试此解决方案?是 MS SQL Server 还是 MS Access?
      • MS SQL Server,但这个例子我在 MS Access 中运行。我将在 MS SQL Server 中尝试。
      猜你喜欢
      • 2021-02-26
      • 2013-11-27
      • 2021-02-12
      • 2012-05-07
      • 2016-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多