请教大侠们一个SQL查询语句
WINFORM程序
菜鸟问题,请教大师们个问题,如下:
我有两个表,第一个表叫mysheet,第二个表叫mysheetout
mysheet有10列,存放编号信息,编号无重复;
比如:
COL1 COL2 COL3...............COL10
产品1 001 002..................
产品2 010 012..............
产品3 021 022...............
mysheetou表 三列,第一列为客户名称,第二列为产品名,第三列为时间
例如:
Col1 Col2 Col3
产品1 客户A 2011-5-6
产品2 客户B 2011-5-7
产品3 客户C 2011-5-8
输入产品1,点击查询,分别从mysheet和mysheetout中 读取A客户购买的产品1对应的 客户名,时间,产品型号,规格等,显示到datagridview中
string commtext1="SELECT Mysheet.*,Mysheetout.* FROM Mysheet,Mysheetout WHERE Mysheet.Col1 LIKE " + "'%" + 产品1 + "%'" + " AND Mysheetout.Col1 LIKE " + "'%" + 产品1 + "%'";SQLiteDataAdapter thisadapter = new SQLiteDataAdapter(commtext1, conn); DataSet thisdataset = new DataSet(); thisadapter.Fill(thisdataset);dataGridView1.DataSource = thisdataset.Tables[0];
select * from Mysheet,MysheetOut where Mysheet.Col1='产品1' and Mysheet.col1=MysheetOut.col1
[解决办法]
写错了,用下面的代码
SqlConnection con = new SqlConnection("数据库连接串"); con.Open(); string sql = string.Format(@"select a.col1,b.col2,b.col3 from mysheet as ainner join mysheetout as b on a.col1=b.col1where (a.col2={0} or a.col3={0} or a.col4={0} or a.col5={0} or a.col6={0} or a.col7={0} or a.col8={0} or a.col9={0} or a.col10={0})",textBox1.Text.Trim()); SqlCommand cmd = new SqlCommand(sql, con); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataSet dst = new DataSet(); sda.Fill(dst); con.Close(); dataGridView1.DataSource = dst.Tables[0];
[解决办法]
LZ主要是需要个查询语句
select a.productName,b.col1,b.col2,b.col3 (select a.productName,a.productCode from(select col1 as productName,col2 as productcode from mysheet
uion all
select col1,col3 from mysheet
union all
………
select col1,col10 from mysheet
) a where a.productCode=控件名.Text.tostring()) a left join mysheetout b on a.productCode=b.col1
LZ如果需要自己完善语句吧,给LZ参考下