英文:
SQL QUERY FOR selecting only products which does not contain one language
问题
我需要仅选择那些*没有语言ID为"en-ZA"*的产品。
在下面的例子中,我需要仅选择产品编号为"102"。
产品编号 | 语言ID |
---|---|
100 | En-Us |
100 | En-ZA |
100 | fr |
101 | En-Us |
101 | En-ZA |
101 | fr |
102 | En-Us |
102 | fr |
英文:
I need to select only those product which is not having language-id "en-ZA".
In the below example I need to select only product number "102".
ProductNumber | Language-ID |
---|---|
100 | En-Us |
100 | En-ZA |
100 | fr |
101 | En-Us |
101 | En-ZA |
101 | fr |
102 | En-Us |
102 | fr |
答案1
得分: 2
你可以通过以下查询来实现这个目的,使用 CASE WHEN
:
SELECT ProductNumber
FROM TableName
GROUP BY ProductNumber
HAVING SUM(CASE WHEN [Language-ID]='En-ZA' THEN 1 ELSE 0 END) = 0
同样的查询也可以使用 NOT EXISTS
来写成下面这样:
SELECT ProductNumber
FROM TableName t1
WHERE NOT EXISTS (
SELECT 1
FROM TableName t2
WHERE [Language-ID] = 'En-ZA'
AND t2.ProductNumber = t1.ProductNumber
)
GROUP BY ProductNumber
英文:
You can achieve this by following query using CASE WHEN
SELECT ProductNumber
FROM TableName
GROUP BY ProductNumber
HAVING SUM(CASE WHEN [Language-ID]='En-ZA' THEN 1 ELSE 0 END) = 0
The same query can be written using NOT EXISTS
like the following.
SELECT ProductNumber
FROM TableName t1
WHERE NOT EXISTS (
SELECT 1
FROM TableName t2
WHERE [Language-ID] = 'En-ZA'
AND t2.ProductNumber = t1.ProductNumber
)
GROUP BY ProductNumber
答案2
得分: 0
尝试识别可能包含 "en-ZA" 的产品编号,使用以下代码:
select distinct ProductNumber
from TABLE
where Language-ID = 'en-ZA'
这将为您提供包含 'en-ZA' 的所有产品编号的列表。
使用该列表,您可以对主列表进行排序,如下所示:
select ProductNumber, Language-ID
from TABLE
where ProductNumber not in (
select distinct ProductNumber
from TABLE
where Language-ID = 'en-ZA'
)
英文:
Try to identify Product numbers which may contain "en-ZA" by using:
select distinct ProductNumber
from TABLE
where Language-ID = 'en-ZA'
That gives you a list of all Product numbers which contain 'en-ZA'.
Using that list you can sort your main list like:
select ProductNumber, Language-ID
from TABLE
where ProductNumber not in (
select distinct ProductNumber
from TABLE
where Language-ID = 'en-ZA'
)
答案3
得分: 0
以下是使用“left join”进行操作的方法:
select t.ProductNumber
from (
select distinct ProductNumber
from mytable
where Language_ID != 'En-ZA'
) t
left join (
select *
from mytable
where Language_ID = 'en-ZA'
) as s on s.ProductNumber = t.ProductNumber
where s.ProductNumber is null
我们确定了Language_ID != 'En-ZA'
(100, 101, 102)的行,并将其与具有Language_ID = 'en-ZA'
(100, 101)的行进行匹配,然后仅选择未匹配的行,使用where s.ProductNumber is null
。
英文:
This is a way to do it using left join
:
select t.ProductNumber
from (
select distinct ProductNumber
from mytable
where Language_ID != 'En-ZA'
) t
left join (
select *
from mytable
where Language_ID = 'en-ZA'
) as s on s.ProductNumber = t.ProductNumber
where s.ProductNumber is null
We determine the rows where Language_ID != 'En-ZA'
(100,101,102) and match it with the ones having Language_ID = 'en-ZA'
(100,101), then select only the ones that are not matched using where s.ProductNumber is null
答案4
得分: 0
使用"NOT EXISTS" - 它简单且相当快速
select ProductNumber, Language-ID
from YourTable as YT1
where not exists (
select 1
from YourTable as YT2
where YT2.Language-ID = 'en-ZA'
and YT2.ProductNumber = YT1.ProductNumber
)
英文:
use NOT EXISTS - it is simple and quite fast
select ProductNumber, Language-ID
from YourTable as YT1
where not exists (
select 1
from YourTable as YT2
where YT2.Language-ID = 'en-ZA'
and YT2.ProductNumber = YT1.ProductNumber
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论