John Lee
John Lee

Reputation: 1387

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?

Upvotes: 0

Views: 46

Answers (1)

zedfoxus
zedfoxus

Reputation: 37049

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")

Upvotes: 1

Related Questions