简体   繁体   中英

How to map a string to a table column in MS Access?

I need an SQL query to map the string content of a column in TableA to a set of columns in TableB. It looks like this:

TableA
| ID | Status |
---------------
| 0  | "full" |
| 1  | "part" |
|    |        |

TableB
| ID | full | part |expire|
--------------------------
| 0  | true | false| false|
| 1  | false| true | false|
|    |      |      |      |

I am using MS Access and currently this is all I've got:

SELECT * FROM TableA INNER JOIN TableB ON TableA.ID = TableB.ID

Which correctly matches the two tables based on ID. However, I do not know where to go from here. Lets say the following is added to TableA:

{ID:2, Status:"Expire"}

How can I map this from TableA to TableB? Ideally this would all be done in 1 query.

Many thanks in advance! :)

Use the IIf() function for each column.

SELECT ID,
    IIf([Status]='full', True, False) AS [full],
    IIf([Status]='part', True, False) AS [part],
    IIf([Status]='expire', True, False) AS [expire]
FROM Table1

Your question is not clear enough.

If you want to recreate TableB from TableA then use the IIf ( expr , truepart , falsepart ) function in a query. Example below (added status field - you can exclude it):

Select ID,Status, IIf([Status]="full", true, false) as full, IIf([Status]="part", true, false) as part,IIf([Status]="expire", true, false) as expire from TableA

If you meant to join the two tables then use LEFT OUTER join that will include all rows in TableA. In this case row 3 will have nulls for the boolean flag since there is no matching row.

SELECT TableA.ID,Status,full,part,expire FROM TableA LEFT JOIN TableB ON TableA.ID = TableB.ID

Regards, Bikxs

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