C# 在mysql中合并两个数据集的结果

C# 在mysql中合并两个数据集的结果,c#,asp.net,mysql,C#,Asp.net,Mysql,我有两个表,table1和table2。我想从table1检索column1,从table2检索column1。然后组合这两个结果,并在栅格视图中显示为单独的列。这是我的密码: string query4 = "select tablename from table1 where tid='1'"; MySqlCommand command4 = new MySqlCommand(query4, connection); DataSet ds = new DataSet(); adapter.

我有两个表,
table1
table2
。我想从
table1
检索
column1
,从
table2
检索
column1
。然后组合这两个结果,并在栅格视图中显示为单独的列。这是我的密码:

string query4 = "select tablename from table1 where tid='1'";
MySqlCommand command4 = new MySqlCommand(query4, connection);

DataSet ds = new DataSet();
adapter.SelectCommand = command4;
adapter.Fill(ds);

GridView1.DataSource = ds;
GridView1.DataBind();
string query5 = "select fname from table2 where id='1'";
MysqlCommand command5 = new MySqlCommand(query5, connection);

DataSet ds2 = new DataSet();
adapter.SelectCommand = command5;
adapter.fill(ds2);

Gridview1.DataSource = ds;

这只给了我
table1
值,但我想在单个网格视图中显示
table1
table2
中的两列


使用
DataSet.Merge()
方法。

在MSDN上快速搜索会出现以下文章

使用
DataSet.Merge()
方法。

内部连接是您的朋友

SELECT
    table1.tablename, 
    table2.fname 
FROM table1 
INNER JOIN table2 
    ON table2.id = table1.tid 
WHERE table1.id = 1
这将在一次查询中获得所有数据

内部连接是您的朋友

SELECT
    table1.tablename, 
    table2.fname 
FROM table1 
INNER JOIN table2 
    ON table2.id = table1.tid 
WHERE table1.id = 1

这将在一个查询中获得所有数据

您可以用两种方法完成。第一个来自MySQL查询,如下所示:

string query6 = "select tablename AS value from table1 where tid='1' UNION select fname from table2 as value where id='1'";

或者使用
DataSet.Merge()将您的
数据集
合并到一个
数据集

您可以通过两种方法完成。第一个来自MySQL查询,如下所示:

string query6 = "select tablename AS value from table1 where tid='1' UNION select fname from table2 as value where id='1'";

或者使用
DataSet.Merge()将您的
数据集
合并到一个
数据集

您是指列还是行?只有下面的UNION响应与将它们作为2行1列返回有关。我的响应(JOIN)涉及将它们作为1行2列返回。我不知道凯文的回答会给你什么样的想法你是指列还是行?只有下面的UNION响应与将它们作为2行1列返回有关。我的响应(JOIN)涉及将它们作为1行2列返回。我不确定凯文的回答会给你什么