如何在groovy中编写字段值可以为null或不为null的查询 公共布尔值IsDuplicateAccountMaterChartOfcountsBySQL(MasterChartOfcounts MasterChartOfcounts){ 字符串查询:“” 挑选 计数(mcoa.id)为mcoaCount 来自mcoa账户的主图表 哪里 mcoa.parent\u account\u id=${masterchartofcounts.parentAccount?.id} 和mcoa.account_code='${masterchartofcounts.accountCode}' 和mcoa.country_id=${masterchartofcounts.countryId} 和mcoa.account_status_id=${AccountsConstants.DOMAIN_status_ACTIVE} """ Sql db=新Sql(数据源) List resultList=db.rows(查询) 返回resultList.get(0).mcoaCount }
此处,字段parent\u account\u id可以为NULL,也可以不为NULL。但sql查询不能以这种方式处理空值。我们必须写作 其中mcoa.parent\u account\u id为空。 那么,在字段可以为NULL或不为NULL的情况下,我该怎么办呢如何在groovy中编写字段值可以为null或不为null的查询 公共布尔值IsDuplicateAccountMaterChartOfcountsBySQL(MasterChartOfcounts MasterChartOfcounts){ 字符串查询:“” 挑选 计数(mcoa.id)为mcoaCount 来自mcoa账户的主图表 哪里 mcoa.parent\u account\u id=${masterchartofcounts.parentAccount?.id} 和mcoa.account_code='${masterchartofcounts.accountCode}' 和mcoa.country_id=${masterchartofcounts.countryId} 和mcoa.account_status_id=${AccountsConstants.DOMAIN_status_ACTIVE} """ Sql db=新Sql(数据源) List resultList=db.rows(查询) 返回resultList.get(0).mcoaCount },sql,groovy,Sql,Groovy,此处,字段parent\u account\u id可以为NULL,也可以不为NULL。但sql查询不能以这种方式处理空值。我们必须写作 其中mcoa.parent\u account\u id为空。 那么,在字段可以为NULL或不为NULL的情况下,我该怎么办呢 public boolean isDuplicateAccountMaterChartOfAccountsBySQL(MasterChartOfAccounts masterChartOfAccounts) { String
public boolean isDuplicateAccountMaterChartOfAccountsBySQL(MasterChartOfAccounts masterChartOfAccounts) {
String query = """
SELECT
COUNT(mcoa.id) as mcoaCount
FROM master_chart_of_accounts as mcoa
WHERE
mcoa.parent_account_id = ${masterChartOfAccounts.parentAccount?.id}
AND mcoa.account_code = '${masterChartOfAccounts.accountCode}'
AND mcoa.country_id = ${masterChartOfAccounts.countryId}
AND mcoa.account_status_id = ${AccountsConstants.DOMAIN_STATUS_ACTIVE}
"""
Sql db = new Sql(dataSource)
List<GroovyRowResult> resultList = db.rows(query)
return resultList.get(0).mcoaCount
}
公共布尔值是双重AccountMaterChartOfcountsBySQL(MasterChartOfcounts MasterChartOfcounts){
字符串查询:“”
挑选
计数(mcoa.id)为mcoaCount
来自mcoa账户的主图表
哪里
mcoa.account_code='${masterchartofcounts.accountCode}'
和mcoa.country_id=${masterchartofcounts.countryId}
和mcoa.account_status_id=${AccountsConstants.DOMAIN_status_ACTIVE}
"""
if(MasterChartOfcounts.parentAccount?.id){
query+=“和mcoa.parent\u account\u id=${masterchartofcounts.parentAccount?.id}”
}
Sql db=新Sql(数据源)
List resultList=db.rows(查询)
返回resultList.get(0).mcoaCount
}
是的,我解决了D
@Transactional(readOnly = true)
public boolean isDuplicateAccountMaterChartOfAccountsBySQL(MasterChartOfAccounts masterChartOfAccounts) {
String query = """
SELECT
COUNT(mcoa.id) as mcoaCount
FROM master_chart_of_accounts as mcoa
WHERE
mcoa.account_code = '${masterChartOfAccounts.accountCode}'
AND mcoa.country_id = ${masterChartOfAccounts.countryId}
AND mcoa.account_status_id = ${AccountsConstants.DOMAIN_STATUS_ACTIVE}
"""
if (masterChartOfAccounts.parentAccount?.id) {
query += " AND mcoa.parent_account_id = ${masterChartOfAccounts.parentAccount?.id}"
}
Sql db = new Sql(dataSource)
List<GroovyRowResult> resultList = db.rows(query)
return resultList.get(0).mcoaCount
}