我试图找出用于将值列表传递给' in'的正确语法。运算符使用GroovySql。当我传入逗号分隔的值列表时,仅应用第一个值而忽略其他值。
我的代码如下:
final String query = '''\
SELECT
li_ch.event_id as event_id,
sum(CASE li_ch.line_item_type WHEN 0 THEN 1 WHEN 900 THEN (CASE li_p.line_item_type WHEN 0 THEN -1 ELSE 0 END) ELSE 0 END) sold,
sum(li_ch.amount) as gross
FROM
line_item li_ch LEFT JOIN line_item li_p on li_ch.parent_line_item_id = li_p.id,
sale s
WHERE
s.id = li_ch.sale_id AND
li_ch.event_id in (${Sql) AND
s.sale_status_type in (:eventIds) AND
li_ch.line_item_type in (0, 900)
GROUP BY li_ch.event_id
'''
// Create new Groovy SQL instance with injected DataSource
final Sql sql = new Sql(dataSource)
def ids = eventList.collect { "'$it'" }.join(",")
final List<GroovyRowResult> rows = sql.rows(query, eventIds: Sql.expand(ids))
rows
答案 0 :(得分:3)
有一个pull request可以让这更容易,但是现在你必须加入你自己的属性:
def values = "'${list.join('\',\'')}'"
// or less confusingly:
// def values = list.collect { "'$it'" }.join(",")
sql.rows( "select * from table where value in (${Sql.expand(values)})" )