Frage

I'm having trouble writing the SQL code to select distinct contacts across 8 columns from an access table. 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.

Keine korrekte Lösung

Andere Tipps

If I have correctly understood, you have contacts in one or more column (from c1 to c8).

If so, try to rewrite your SQL statement like the following:

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.

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. Try doing this:

cmd.CommandText = "select distinct c1, c2, c3, c4, c5, c6, c7, c8 
                   from table 
                   where state IN ('" + paramClause.ToString() + "')";
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top