如何从一个表到另一个表中选择id [英] how to select id from one table to anothter table
本文介绍了如何从一个表到另一个表中选择id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
先生如何从一张桌子中选择id并保存在另一张桌子上........
我的代码....
MySqlConnection exclecon = new MySqlConnection( Server =本地主机;数据库=密码1;用户=根;密码=甘露跨度>);
exclecon.Open();
MySqlCommand cmdYear = new MySqlCommand( INSERT INTO slayear(YEAR)VALUES(@YEAR),exclecon);
cmdYear.Parameters.AddWithValue( @YEAR,YEAR);
MySqlCommand cmdMonth = new MySqlCommand( INSERT INTO slamonth(MONTH,contenttype)VALUES(@ MONTH,@ contenttype)=(选择来自slayear的idyear,其中idyear = @ idyear),exclecon);
cmdMonth.Parameters.AddWithValue( @ MONTH,MONTH);
cmdMonth.Parameters.Add( @ contenttype,MySqlDbType.VarChar).Value =内容类型;
cmdYear.ExecuteNonQuery();
cmdMonth.ExecuteNonQuery();
已添加代码块[/ Edit]
解决方案
希望年份在您的slayear表中是唯一的,您可以使用另一个命令,用于检索您插入的同一年的ID。
我的建议是尝试创建一个insertprocedure,它插入年份并返回ID或更新您的月份详细信息以及
sir how o select id from one table and save in another table ........
my code....
MySqlConnection exclecon = new MySqlConnection("Server=Localhost;DataBase=password1;user=root;password=nectar");
exclecon.Open();
MySqlCommand cmdYear = new MySqlCommand("INSERT INTO slayear (YEAR) VALUES(@YEAR)", exclecon);
cmdYear.Parameters.AddWithValue("@YEAR", YEAR);
MySqlCommand cmdMonth = new MySqlCommand("INSERT INTO slamonth (MONTH,contenttype) VALUES(@MONTH,@contenttype)=(select idyear from slayear where idyear=@idyear) ", exclecon);
cmdMonth.Parameters.AddWithValue("@MONTH", MONTH);
cmdMonth.Parameters.Add("@contenttype", MySqlDbType.VarChar).Value = contenttype;
cmdYear.ExecuteNonQuery();
cmdMonth.ExecuteNonQuery();
[Edit]Code block added[/Edit]
解决方案
Hoping Year is unique in your slayear table, You can use another command to retrieve id for the same year you have inserted.
My suggestion is to try creating a storedprocedure which inserts year and returns the ID or updates your month details aswell.
这篇关于如何从一个表到另一个表中选择id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文