简体   繁体   中英

MS Access SQL Query with condtional statement

I am trying to update a table using values from a mapping table. I think its an UPDATE and SELECT statement but I cant figure out how to handle conditions. This is in VBA Access.

I have two tables with following fields:

mainTable: [column1],[column2],[xcode],[ycode]
mapTable: [xcode],[ycode],[mapping]

I need to do a mapping from mapTable but it has the following condition

If mainTable.[column1] = "000" or mainTable.[column1] = "001" Then
  mainTable.[column2] = mapTable.[mapping]
  WHERE mapTable.[xcode] = mainTable.[xcode] 
  AND mapTable.[ycode] = mainTable.[ycode]
Else
  mainTable.[column2] = mapTable.[mapping]
  WHERE mapTable.[xcode] = mainTable.[xcode] 
End If

Is there a way to capture this in a single SQL query?

You will need two queries and that's a good thing: one query gets you the results that you would get with if and the other query gives you the results that you would get with else .

Query1:

update maintable m inner join maptable map on m.xcode = map.xcode and m.ycode = map.ycode
set m.column2 = map.mapping
where m.column1  = "000" or m.column1 = "001"

Query2:

update maintable m inner join maptable map on m.xcode = map.xcode
set m.column2 = map.mapping
where NOT (m.column1  = "000" or m.column1 = "001")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM