Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 动态属性名称搜索_Java_Sql_Oracle_Jpa - Fatal编程技术网

Java 动态属性名称搜索

Java 动态属性名称搜索,java,sql,oracle,jpa,Java,Sql,Oracle,Jpa,假设我有这样一张学生桌: student_id name address ... school employer 1 Chris 2 John UofJ J Limited 2 Ann 3 Doe UofD D limited 现在我需要找到一份学生名单,他们拥有学校='UofJ'和雇主='J Limited'。简单: select * from student where s

假设我有这样一张学生桌:

student_id   name   address ...   school   employer
    1      Chris    2 John         UofJ     J Limited
    2      Ann      3 Doe          UofD     D limited
现在我需要找到一份学生名单,他们拥有
学校='UofJ'
雇主='J Limited'
。简单:

select * from student where school = 'UofJ' and employer = 'J Limited'
然而,我的实际情况是,最后两个属性作为列存储在学生表中,但作为行存储在名为
student\u attribute
的单独表中:

student_attribute_id    student_id    attribute_name    attribute_value
       1                    1            school            UofJ
       1                    1            company           J Limited
       1                    2            school            UofD
       1                    2            company           D Limited
我的任务是根据
学校='UofJ'
雇主='J有限公司'
从该
学生属性
表中查找学生ID列表。我该怎么做


此外,我正在使用Springboot JPS存储库进行查询,因此我愿意听取sql方式或JPA方式的解决方案。

为您关心的每个属性设置联接:

select * from student s
join student_attribute school on school.student_id = s.student_id 
join student_attribute company on company.student_id = s.student_id 
where company.attribute_value='J Limited'
and school.attribute_value='UofJ'

您可以使用条件聚合来找出哪个学生id同时具有这两个条件true

select student_id
from student_attribute
group by student_id
having count(case 
            when attribute_name = 'school'
                and attribute_value = 'UofJ'
                then 1
            end) > 0
    and count(case 
            when attribute_name = 'company'
                and attribute_value = 'J Limited'
                then 1
            end) > 0
然后,您可以将其与student表联接,以获得相应的详细信息

select s.*
from student s
join (
    select student_id
    from student_attribute
    group by student_id
    having count(case 
                when attribute_name = 'school'
                    and attribute_value = 'UofJ'
                    then 1
                end) > 0
        and count(case 
                when attribute_name = 'company'
                    and attribute_value = 'J Limited'
                    then 1
                end) > 0
    ) a on s.student_id = a.student_id;

属性值是包含不同类型的字符串吗?您总是在搜索字符串,还是还需要搜索数字和日期?