gpt4 book ai didi

c# - 如何从数据库中检索非空和非重复数据?

转载 作者:太空宇宙 更新时间:2023-11-03 20:55:37 25 4
gpt4 key购买 nike

我的表上有这些挑剔的列(因为其余列与问题无关)。

ID   | Generic Name
-----+---------------
001 | Cetirizine
002 | Cetirizine
003 |
004 | Paracetamol

我希望我的组合框仅显示单个条目 Cetirizine(或任何已重复的数据)并且没有空的通用名称(某些数据没有通用名称)。

我试过:

select 
Item_GenName
from
ItemMasterlistTable
where
nullif(convert(varchar, Item_GenName), '') is not null

但它只实现了无空数据部分。

我尝试过使用 DISTINCT,但它不起作用,有人建议使用 JOIN,但我认为它不起作用,因为我只使用了 1 个表.

我也试过:

SELECT 
MIN(Item_ID) AS Item_ID, Item_GenName
FROM
ItemMasterlistTable
GROUP BY
Item_GenName

但是总是报错:

The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator.

最佳答案

以下查询应仅返回不同的、非空的 Item_GenNames:

SELECT DISTINCT Item_GenName
FROM ItemMasterlistTable
// because Item_GenName is of type *text*, the below in lieu of `is not null` and `!= ''`
WHERE datalength(Item_GenName) != 0

你说你试过 DISTINCT 但它没有用所以我想澄清一下,

DISTINCT 关键字将在您的选择语句的整个域中返回唯一记录。如果您在选择语句中包含 ID 列,即使是不同的选择也会返回重复的 Item_GenNames b/c,组合的 ID/Item_GenName 记录将是唯一的。在您的 select 子句中仅包含 Item_GenName 以保证此列的不同值。

关于c# - 如何从数据库中检索非空和非重复数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50885871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com