Querying table and selecting on a few of the columns included in a primary key is generating a DISTINCT result. #1484
Unanswered
mithrandyr
asked this question in
Q&A
Replies: 2 comments 5 replies
-
I can't reproduce this problem using standard C# to use the ADO.NET interfaces var connectionString = "server=localhost;database=mysqltest;userid=root;password=pass";
using var mscConnection = new MySqlConnector.MySqlConnection(connectionString);
mscConnection.Open();
using var msdConnection = new MySql.Data.MySqlClient.MySqlConnection(connectionString);
msdConnection.Open();
using (var mscCommand = mscConnection.CreateCommand())
{
mscCommand.CommandText = """
DROP TABLE IF EXISTS test3;
DROP TABLE IF EXISTS test4;
-- Here is the SQL to create the two tables
-- Primary key
CREATE TABLE test3 (colA VARCHAR(25), colB VARCHAR(25), colC VARCHAR(25), colD VARCHAR(25), PRIMARY KEY (colA, colB, colC));
INSERT INTO test3 (colA, colB, colC, colD) VALUES ('A', 'A', 'A', 'A');
INSERT INTO test3 (colA, colB, colC, colD) VALUES ('A', 'B', 'A', 'A');
INSERT INTO test3 (colA, colB, colC, colD) VALUES ('B', 'A', 'A', 'A');
INSERT INTO test3 (colA, colB, colC, colD) VALUES ('C', 'A', 'A', 'A');
-- No primary Key
CREATE TABLE test4 (colA VARCHAR(25), colB VARCHAR(25), colC VARCHAR(25), colD VARCHAR(25));
INSERT INTO test4 (colA, colB, colC, colD) VALUES ('A', 'A', 'A', 'A');
INSERT INTO test4 (colA, colB, colC, colD) VALUES ('A', 'B', 'A', 'A');
INSERT INTO test4 (colA, colB, colC, colD) VALUES ('B', 'A', 'A', 'A');
INSERT INTO test4 (colA, colB, colC, colD) VALUES ('C', 'A', 'A', 'A');
""";
mscCommand.ExecuteNonQuery();
}
foreach (var connection in new DbConnection[] { mscConnection, msdConnection })
{
foreach (var sql in new[] { "SELECT colA FROM test3", "SELECT colA FROM test4" })
{
using var command = connection.CreateCommand();
command.CommandText = sql;
Console.WriteLine("{0} --- {1}", command.CommandText, command.GetType().FullName);
using var reader = command.ExecuteReader();
{
while (reader.Read())
{
Console.WriteLine(reader.GetValue(0));
}
}
Console.WriteLine();
}
} Output:
|
Beta Was this translation helpful? Give feedback.
1 reply
-
It looks like you might be using I rewrote the code to test that instead: using var da = command is MySqlConnector.MySqlCommand ?
(DbDataAdapter) new MySqlConnector.MySqlDataAdapter((MySqlConnector.MySqlCommand) command) :
new MySql.Data.MySqlClient.MySqlDataAdapter((MySql.Data.MySqlClient.MySqlCommand) command);
Console.WriteLine("{0} --- {1}", command.CommandText, da.GetType().FullName);
var ds = new DataSet();
da.ReturnProviderSpecificTypes = true;
da.Fill(ds);
foreach (DataRow row in ds.Tables[0].Rows)
Console.WriteLine(row[0]);
Console.WriteLine(); Same output:
|
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
All - could use some help trying to figure out why this is happening. I've written a PowerShell module that wraps various database providers to make it easier to interact with databases through PowerShell (the module is called SimplySql). Anyways, I did a major rewrite last year and in the process changed my MySql provider to MySqlConnector. One of my users has reported this issue.
Basically, if a table is created with a primary key with multiple columns, but a select statement with only primary key columns included (but not all of them), then a DISTINCT result is happening.
Ok, now here is the PowerShell and the interesting results
Beta Was this translation helpful? Give feedback.
All reactions