如何从2表中选择并使用mysqldatareader获取它们的值
问题描述:
上午好女士和先生。我想从表1中选择原因并从table2中选择地址,其中unique_id = @uniqueid。两个表都有名为unique_id的主键。并使用DataReader获取它们的价值。
我尝试过:
Good day Ma'am and Sir. I want to select cause from table 1 and select address from table2 where unique_id = @uniqueid. Both tables have primary key called unique_id. and get the their value using DataReader.
What I have tried:
using(MySqlConnection con = new MySqlConnection(connString))
{
consp.Open();
string clientid = "009";
MySqlDataReader reader;
MySqlCommand com = new MySqlCommand("SELECT FROM ",con);
com.Parameters.AddWithValue("@uniqueid,"clientid);
reader = com.ExecuteReader();
while(reader.HasRows && reader.Read())
{
string cause;
string address;
cause = reader.GetString(reader.GetOrdinal());
address = reader.GetString(reader.GetOrdinal());
}
}
答
为什么不使用JOIN语句?
注意:我没有测试过这个查询,打算将其发布在评论上但不起作用。
Why not use a JOIN statement?
Note: I had not tested this query, intended to post it on comment but not working.
MySqlCommand com = new MySqlCommand("SELECT clause, address FROM table1 JOIN table2 ON table1.unique_id = table2.unique_id WHERE table1.unique_id = @uniqueid",con);