简体   繁体   English

SQL-在多个列中选择不同的

[英]SQL - select distinct across multiple columns

I'm having trouble writing the SQL code to select distinct contacts across 8 columns from an access table. 我在编写SQL代码以从访问表中的8列中选择不同的联系人时遇到了麻烦。 I want to only get the distinct results to not have duplicates. 我只想得到不重复的不同结果。 Below is what I've tried. 以下是我尝试过的方法。

cmd.CommandText = "select distinct c1, c2, c3, c4, c5, c6, c7, c8 
                   from table 
                   where state IN (" + paramClause.ToString() + ")";

My purpose is to show this on a label with no duplicates. 我的目的是在没有重复的标签上显示此内容。

If I have correctly understood, you have contacts in one or more column (from c1 to c8). 如果我正确理解,则您在一个或多个列(从c1到c8)中有联系人。

If so, try to rewrite your SQL statement like the following: 如果是这样,请尝试如下重写您的SQL语句:

SELECT C1
FROM TABLE
WHERE STATE IN (...)
UNION
SELECT C2
FROM TABLE
WHERE STATE IN (...)
...
UNION
SELECT C8
FROM TABLE
WHERE STATE IN (...)

The UNION operator eliminates duplicates by itself. UNION运算符可自行消除重复项。

I hope this helps you.... 我希望这可以帮助你....

You have: 你有:

cmd.CommandText = "select distinct c1, c2, c3, c4, c5, c6, c7, c8 
                   from table 
                   where state IN (" + paramClause.ToString() + ")";

But you need to wrap strings in single quotes. 但是您需要将字符串用单引号引起来。 I think the problem is in your WHERE clause. 我认为问题出在您的WHERE子句中。 Try doing this: 尝试这样做:

cmd.CommandText = "select distinct c1, c2, c3, c4, c5, c6, c7, c8 
                   from table 
                   where state IN ('" + paramClause.ToString() + "')";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM