开发者

Select or leave a field blank dependent on a condition without affecting query

开发者 https://www.devze.com 2023-03-11 02:03 出处:网络
I would like to select a field but only under specific circumstance, otherwise i would like it to just be blank or null.Example

I would like to select a field but only under specific circumstance, otherwise i would like it to just be blank or null. Example

Select
a.Name,
c.Hat as FancyHat,
b.Shirt,
b.Shoes,
c.Hat

from
directory a, store b, superHatStore c

where 
a.key = b.key
a.key = c.key

now 开发者_JAVA技巧I would only want to display a FancyHat name if it is a 'fancy_hat' example

where c.Type = 'fancy_hat'

but if I put it in the where clause it is too restrictive... because I also want the name of the hat to be in another field regardless of hat type... maybe I am just doing something wrong. Any suggestions would be greatly appreciated, Thanks


For MS-Access you can use either the IIF statement
http://www.techonthenet.com/access/functions/advanced/iif.php

or the CASE statement (if you're using VBA)
http://www.techonthenet.com/access/functions/advanced/case.php

EDIT: So if I'm reading correctly you want (UNTESTED!):

Select
a.Name,
iif (c.[type] = "fancy_hat", c.hat, NULL) as FancyHat,
b.Shirt,
b.Shoes,
c.Hat

from
directory a, store b, superHatStore c

where 
a.key = b.key
a.key = c.key
0

精彩评论

暂无评论...
验证码 换一张
取 消