UPDATE statement with multiple WHERE conditions

前端 未结 2 1547
一整个雨季
一整个雨季 2021-02-04 04:32

I\'m trying to run an UPDATE query in Access with different WHERE conditions:

UPDATE Table1
SET [Ticker] = \"TXSFI\" WHERE [Acct Numb]          


        
2条回答
  •  [愿得一人]
    2021-02-04 05:07

    You can do this with two update statement:

    UPDATE Table1
        SET Table1.[Ticker] = 'TXSFI' WHERE Table1.[Acct Numb] like '*03';
    
    Update Table1
        SET Table1.[Ticker] = 'TESEI' WHERE Table1.[Acct Numb] like '*04';
    

    Or, you can combine these using conditional updates (non-Access version):

    update table1
       SET Table1.[Ticker] = (case when  Table1.[Acct Numb] like '%03' then 'TXSFI'
                                   when Table1.[Acct Numb] like '%04' then 'TESEI'
                              end)
       where Table1.[Acct Numb] like '%03' or Table1.[Acct Numb] like '%04'
    

    By the way, I am guessing that you are using Access. The standard wildcard in SQL would be like '%03' but Access uses a '*' instead of '%'. It is a good idea to tag your question with the database you are using.

    Having said that, you can't use case in Access:

    update table1
       SET Table1.[Ticker] = iif(Table1.[Acct Numb] like '*03', 'TXSFI', 'TESEI')
       where Table1.[Acct Numb] like '*03' or Table1.[Acct Numb] like '*04'
    

提交回复
热议问题