我有两对的三个整数值。
我想将其用作WHERE子句中IN的列表。
(2019,5)(2019,6)(2019,7)
我想在这样的查询中使用上述两对列表:
SELECT
*
FROM
WEEK_REPORT A
WHERE
A.user_id IN ('test2','test5' ) AND
(A.year, A.week_no) IN ((2019,4),(2019,5),(2019,6));
为此,我将源代码编写如下:
// lastYear=2019, lastWeekNo=4
Tuple t = JPAExpressions.select(Expressions.constant(lastYear), Expressions.constant(lastWeekNo))
.from(weekReport)
.fetchOne();
// currentYear=2019, currentWeekNo=5
Tuple t2 = JPAExpressions.select(Expressions.constant(currentYear), Expressions.constant(currentWeekNo))
.from(weekReport)
.fetchOne();
// nextYear=2019, nextWeekNo=4
Tuple t3 = JPAExpressions.select(Expressions.constant(nextYear), Expressions.constant(nextWeekNo))
.from(weekReport)
.fetchOne();
return queryFactory
.select(weekReport)
.from(weekReport)
.where(weekReport.user.eq(user)
.and(Expressions.list(weekReport.year, weekReport.weekNo).in(t, t2, t3)))
.fetch();
但是,不会输出正确的结果,并且会发生错误。
java.lang.UnsupportedOperationException: null
at com.querydsl.jpa.JPASubQuery.fetchOne(JPASubQuery.java:66) ~[querydsl-jpa-4.1.4.jar:na]
我在正式文件中进行了查询,但没有出来。
有办法吗?
谢谢。
答案 0 :(得分:0)
尝试
SELECT
*
FROM
WEEK_REPORT A
WHERE
A.user_id IN ('test2','test5' ) AND
concat(A.year, A.week_no) IN (20194,20195,20196);
答案 1 :(得分:0)
要在IN子句中管理元组,可以将子查询与工会全部一起使用 对于元组,您可以将子查询与union all一起使用
SELECT
*
FROM
WEEK_REPORT A
WHERE
A.user_id IN ('test2','test5' ) AND
(A.year, A.week_no) IN (
select year, month from (
select 2019 year, 4 week from dual
union all
select 2019 , 5 from dual
union all
select 2019 , 6 from dual ) t
);
答案 2 :(得分:0)
如何将元组列表作为一个参数传递
List<Tuple> params = ...
params = [(2019,4),(2019,5),(2019,6)]
return queryFactory
.select(weekReport)
.from(weekReport)
.where(weekReport.user.eq(user)
.and(Expressions.list(weekReport.year, weekReport.weekNo).in(params)))
.fetch();
答案 3 :(得分:0)
最后,为了从数据库中获取值到持久化上下文中,我们通过调用 fetchOne() 方法结束构建链。如果找不到对象,则此方法返回 null,但如果有多个实体满足 .where() 条件,则抛出 NonUniqueResultException。
当您调用 fetchOne() 时,您的数据库似乎不包含您请求的值。
为了验证您的 sql 是否正确,我运行了它。
SELECT * FROM WEEK_REPORT A WHERE
A.user_id IN ('test2','test5' ) AND
(A.year, A.week_no) IN ((2019,4),(2019,5),(2019,6));
+---------+------+---------+
| user_id | year | week_no |
+---------+------+---------+
| test2 | 2019 | 5 |
| test5 | 2019 | 6 |
+---------+------+---------+
2 rows in set (0.00 sec)
答案 4 :(得分:0)
您要查找的内容有时称为行变量或元组变量。您的 DBMS 可能没有这样的生物。
一般来说,IN
只是 EXISTS
的简写,你可以用很长的方式去剥那只猫的皮:
SELECT * FROM WEEK_REPORT as A
WHERE
A.user_id IN ('test2','test5' )
and exists (
select 1 from (
select 2019 as 'year', 4 as 'week'
union
select 2019, 5
union
select 2019, 6
) as T
where week = A.week and year = A.year
);
还有其他方法。基本上,您希望将行变量放入某种表中,然后测试该表中是否存在。如果是我,随着时间的推移,年和周会发生变化,我会创建一个静态表来保存 3 行,这样我就可以在不更改查询的情况下修改搜索条件。
答案 5 :(得分:0)
DECLARE @t TABLE (
year int,
week int
)
INSERT @t
VALUES (2019, 4), (2019, 5), (2019, 6)
SELECT
*
FROM WEEK_REPORT A
WHERE A.user_id IN ('test2', 'test5')
AND EXISTS (SELECT
*
FROM @t T
WHERE A.year = T.year
AND T.week = A.week_no)