简体   繁体   English

如果Field1中的值为null,则从Field2中返回值,否则返回String

[英]If value in Field1 is null, return value from Field2, otherwise Return String

Although this seems simple enough, I cannot get it, nor was I able to find what I needed via searching on here. 尽管这看起来很简单,但我无法理解,也无法通过在此处搜索找到所需的内容。 I need this to be done within the builder, or within my SQL code which is already an extremely lengthy bit of code for what the query is doing already. 我需要在构建器中或在我的SQL代码中完成此操作,对于已经在执行的查询,这已经是非常冗长的代码。 Here is the basis of the issue: 这是问题的基础:

Fields: ItemID | 字段: ItemID | CustomID

If there is a value in CustomID , I'd like to overwrite the ItemID value for that record with a string: "CK"+CustomID . 如果CustomID有一个值,我想用字符串"CK"+CustomID覆盖该记录的ItemID值。

If the CustomID field is empty for that record, then keep the original ItemID . 如果该记录的CustomID字段为空,则保留原始ItemID

I then also need the value for that field to be used to pull the correct data from a linked table. 然后,我还需要该字段的值,以用于从链接表中提取正确的数据。 Currently, it will pull the original ItemID 's values for subsequent fields, rather than the string "CK+CustomID" value that would be substituted in place of ItemID . 当前,它将为后续字段提取原始ItemID的值,而不是将替换ItemID的字符串"CK+CustomID"值。

I thought perhaps an independent subquery would be necessary to discern what the field's value would be, before needing to use that value to gather the subsequent fields from the lookup table. 我认为也许需要一个独立的子查询来识别该字段的值,然后才需要使用该值从查找表中收集后续字段。

I am willing to work with the SQL code generated by my query builder, but I am not the greatest with just the code. 我愿意使用查询生成器生成的SQL代码,但我并不是仅凭代码就可以做到最好。

Thank you for your help! 谢谢您的帮助!

Without seeing a sample of the content of your table or linked table, nor knowing the names of either of these tables, I would suggest the following example code: 在没有看到表或链接表内容的样本,也不知道这些表中任何一个表的名称的情况下,我建议使用以下示例代码:

select * 
from yourlinkedtable t inner join
(
    select iif(t.customid is null, t.itemid, 'CK' & t.customid) as id
    from yourtable t
) q 
on t.itemid = q.id

Here, yourtable represents the table containing the ItemID and CustomID fields, and yourlinkedtable represents the table with which your are looking to match the prefixed ItemID . 在这里, yourtable代表包含ItemIDCustomID字段的表,并且yourlinkedtable代表要与之匹配的前缀ItemID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL(DQL):WHERE值IN(数组field1)或field2 =值 - SQL (DQL): WHERE value IN (array field1) OR field2 = value MS SQL:从[表]中选择field1,field2,其中,当field1 ='value2'时,field2 NOT IN('val1',val2') - MS SQL: SELECT field1, field2 from [table] where field2 NOT IN ('val1', val2') when field1 = 'value2' MSACCESS:选择“多少天前” {可以返回:今天,昨天,2天前,…},field1,field2,fieldN FROM mytable - MSACCESS: SELECT “How many days ago” {can return: today, yesterday, 2 days ago, …}, field1, field2, fieldN FROM mytable SQL WHERE Field1> = 1或Field2> = 1允许(空)值 - SQL WHERE Field1 >= 1 OR Field2 >= 1 Allowing (null) Values SQL的最佳实践JOIN如果Field1 IS为NULL,则为Field2 ELSE Field1 - Best practices for SQL JOIN ON IF Field1 IS NULL then Field2 ELSE Field1 从单个表中检查field1和field2 - check field1 and field2 from single table 参数化查询中的(?,?...?)或(@ field1,@ field2 ... @ fieldn)? - (?,?…?) or (@field1,@field2…@fieldn) in parmeterized queries? (MySQL)OrderBy Field1 = 3,Field2 - (MySQL) OrderBy Field1=3, Field2 复制field2时更新field1 - Update field1 when field2 is duplicated MYSQL (field1, field2) = (x,y) vs field1= x AND field2 = Y 之间的差异 - MYSQL Difference between ( field1, field2 ) = (x,y) vs field1= x AND field2 = Y
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM