如果所有ID都匹配,则SQL查询返回结果

时间:2016-03-03 15:30:50

标签: sql oracle

我有两张桌子:

COUNTRY,其中包含两列COUNTRY_IDPERSON_REGION_ID

PERSON,包含许多列,其中PERSON_REGION_ID列与COUNTRY.PERSON_REGION_ID相同,而PERSON_IDPERSON的ID列。

查询如下:

SELECT *
from COUNTRY
where PERSON_REGION_ID IN (
        SELECT PERSON_REGION_ID
        FROM PERSON
        WHERE PERSON_ID IN (111, 888)))
  AND COUNTRY_ID = 44;

如果任何一个ID匹配(111或888),则上述查询会给出结果。

如果111和888都匹配,我希望查询给出结果,否则不返回结果。

如何实现这一目标?

3 个答案:

答案 0 :(得分:1)

我更喜欢在这里使用连接

编辑:要回答您的评论,这取决于它的程序还是仅查询。但你声明变量并使用

顺便说一下,只是为了记录......这是T-SQL,而不是oracle的语法

declare @CountryID int -- = 44? (if for some reason you keep CountryID as type other 
                       --      then int, just change it to correct one)
declare @Person1 int -- = 111?
declare @Person2 int -- = 888?

select C.* from Country C
join Person P1 on C.Person_Region_ID = P1.PersionRegion_ID and P1.Country_ID = @CountryID
join Person P2 on C.Person_Region_ID = P2.PersionRegion_ID and P2.Country_ID = @CountryID
where P1.PersionID = @Person1 and P2.PersionID = @Person2

答案 1 :(得分:0)

一种选择是在子查询中使用group byhaving

select * 
from COUNTRY 
where PERSON_REGION_ID IN (
      select PERSON_REGION_ID 
      from PERSON
      where PERSON_ID IN ( 111, 888))
      group by PERSON_REGION_ID 
      having count(PERSON_ID) = 2)
   and COUNTRY_ID= 44;

如果每person_id's个重复PERSON_REGION_ID,则您需要distinct使用count

答案 2 :(得分:0)

你可以使用这样的EXISTS():

#include <opencv2/core.hpp>
#include <opencv2/core/ocl.hpp>

#include <fstream>
#include <sstream>

#include <chrono>

    int main()
    {
        cv::Mat_<unsigned short> a(480,640);

        cv::RNG rng(std::time(nullptr));

        std::for_each(a.begin(),a.end(),[&](unsigned short& v){ v = rng.uniform(0,100);});

        bool ret = false;

        cv::String file_content;

        {
            std::ifstream file_stream("../test/histogram.cl");
            std::ostringstream file_buf;

            file_buf<<file_stream.rdbuf();

            file_content = file_buf.str();
        }

        int output_flag = cv::ocl::Device::getDefault().doubleFPConfig() == 0 ? CV_32F : CV_64F;

        cv::String atomic_fun = output_flag == CV_32F ? "Atomic_Add_f32" : "Atomic_Add_f64";

        cv::ocl::ProgramSource source(file_content);

    //    std::cout<<source.source()<<std::endl;

        cv::ocl::Kernel k;

        cv::UMat src;
        cv::UMat dst = cv::UMat::zeros(1,65536,output_flag);

        a.copyTo(src);

        atomic_fun = cv::format("-D _Sty=%s -D _Rty=%s -D _Dty=%s -D ATOMIC_FUN=%s",
                                cv::ocl::typeToStr(src.depth()),
                                cv::ocl::typeToStr(src.depth()), // this to manage case like a matrix of usigned short stored as a matrix of float.
                                cv::ocl::typeToStr(output_flag),
                                atomic_fun.c_str());


        ret = k.create("khist",source,atomic_fun);

        std::cout<<"check create : "<<ret<<std::endl;

        k.args(cv::ocl::KernelArg::ReadOnly(src),cv::ocl::KernelArg::WriteOnlyNoSize(dst));

        std::size_t sz = a.rows;

        ret = k.run(1,&sz,nullptr,false);

        std::cout<<"check "<<ret<<std::endl;

        cv::Mat b;

        dst.copyTo(b);

std::copy_n(b.ptr<double>(0),101,std::ostream_iterator<double>(std::cout," "));
std::cout<<std::endl;


        return EXIT_SUCCESS;
    }