简体   繁体   中英

MS Access IIF using IN condition

Im trying to add a field into a table that indicates whether the value exists in another table. Heres what I have

table1:     table2:
id          id  
1           2
2           4
3           6

End result:

table3:
id   inTable2
1    no
2    yes
3    no

Heres what I have so far.

SELECT id, IIf(id In (SELECT id FROM table2 GROUP BY id), "yes", "no") AS inTable2 INTO table3

I'm getting error "An action query cannot be used by a rowsource". Any help would be appreciated, thanks!

You are close. The group by is unnecessary:

IIf(id In (SELECT id FROM table2), "yes", "no")

However, I'm not 100% sure this will fix your problem. This will work in a SELECT query, but your error suggests a different type of query.

You can try putting an index on table2(id) . However, I might write this as:

iif(exists (select 1 from table2 where table2.id = ?.id), "yes", "no")

This should take advantage of an index on table2(id) .

You can probably optimize this query by using a LEFT JOIN instead:

SELECT t1.id, IIF(t2.id IS NULL, "yes", "no") INTO table3
FROM table1 t1
LEFT JOIN table2 t2 ON t1.ID = t2.ID

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