3

我有一个查询,只有当 WHERE 子句中的所有 ID 都匹配时,我才需要获取一行

这是查询

SELECT CompanyId
FROM vendors
WHERE VendorId
IN (
'306145244', '1011073437'
)

我想要所有CompanyId同时具有vendorId“306145244”和“1011073437”的

如果其中VendorId一场比赛,我不想要那个companyId。两个 VendorId 都应该匹配

分组时,我需要 2 行,如下所示

company Id   Vendor Id
1             306145244
1             1011073437
2             306145244
2             306145244

如果可能的话,我希望在ORACLE.

4

5 回答 5

2
SELECT  CompanyId
FROM    vendors
WHERE   VendorId IN ('306145244', '1011073437')
GROUP   BY CompanyId
HAVING  COUNT(*) = 2

其他链接:


如果vendorID不是每个都唯一CompanyIdDISTINCT则关键字只需要计算唯一值,

SELECT  CompanyId
FROM    vendors
WHERE   VendorId IN ('306145244', '1011073437')
GROUP   BY CompanyId
HAVING  COUNT(DISTINCT VendorId) = 2
于 2013-04-25T05:46:43.203 回答
0

尝试这个 :

SELECT CompanyId,VendorId
FROM vendors
WHERE VendorId
IN (
select VendorId from vendors
)
order by CompanyId asc;

希望它会帮助你。

于 2013-04-25T06:49:02.890 回答
0

您可能需要查看 ALL。

SELECT CompanyId
FROM vendors
WHERE VendorId = ALL (
    query to get numbers.
)

http://dev.mysql.com/doc/refman/5.5/en/all-subqueries.html

于 2013-04-25T05:46:37.337 回答
0

我们SQL Server能做的是:

SELECT CompanyId
FROM vendors
WHERE VendorId
IN ('306145244')
intersect
SELECT CompanyId
FROM vendors
WHERE VendorId
IN ('1011073437')

MYSQL不支持intersect。因此,我们必须在 MYSQL 中寻找相交的替代方法

select distinct CompanyId
FROM vendors
where vendorId IN ('306145244')
and CompanyId IN
(SELECT CompanyId
FROM vendors
WHERE VendorId
IN ('1011073437'))

编辑

select  CompanyId,VendorId
FROM vendors
where vendorId IN ('306145244')
and CompanyId IN
(SELECT CompanyId
FROM vendors
WHERE VendorId
IN ('1011073437'))
union
select  CompanyId,VendorId
FROM vendors
where vendorId IN ('1011073437')
and CompanyId IN
(SELECT CompanyId
FROM vendors
WHERE VendorId
IN ('306145244'))

SQL小提琴

于 2013-04-25T06:08:01.807 回答
0
SELECT `CompanyId` , count( `CompanyId` )
FROM `vendors`
WHERE VendorId
IN (
'306145244', '1011073437'
)
GROUP BY `CompanyId`
HAVING count( `CompanyId` ) = 2;

使用这个,这对我有用并经过测试。

于 2013-04-25T05:52:15.773 回答