如何连接两个表并使用2个下拉列表过滤结果 [英] how to join two tables and filter the result using 2 dropdownlists

查看:71
本文介绍了如何连接两个表并使用2个下拉列表过滤结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  string  str =  string  .Format( @ 选择m.autoid,m.pcode,m.fyyear,m.date,m.salary,m.ta,m.contigency ,m.nrc,m.institcharges,m.others,y.yearlyalloc,y.salary,y.ta,y.contigency,y.nrc,y.institcharges,y.others FROM每月AS m内部加入年度y pcode where m.pcode =(' + DropDownList1.SelectedItem.ToString()+  ')和y。 fyyear =(' + DropDownList2.SelectedItem.ToString()+  ') ,con); 
SqlDataAdapter da = new SqlDataAdapter(str,con);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();





i能够过滤数据,但我无法做到的是如何将两个表连接在一起使用它们与dropdownlist条件

解决方案

查看INNER JOIN的语法



  SELECT  ft.col1,ft.col2,st.col1,st.col2 
FROM FristTable ft
INNER JOIN SecondTable st
ON ft.colname = st.colname





在您的查询中没有ON关键字...


更正内部联接查询使用on关键字连接两个表

示例:

 选择 *  FROM 每月 AS  m  inner  加入每年y   m.pcode = y.pcode 其中​​ m.pcode =(' + DropDownList1.SelectedItem.ToString()+' AND  y.fyyear =(' + DropDownList2.SelectedItem.ToString()+  


string str = string.Format(@"select m.autoid,m.pcode, m.fyyear, m.date, m.salary, m.ta, m.contigency, m.nrc, m.institcharges, m.others,y.yearlyalloc,y.salary,y.ta,y.contigency,y.nrc,y.institcharges,y.others FROM monthly AS m inner join yearly y pcode where m.pcode=('" + DropDownList1.SelectedItem.ToString() + "' ) AND y.fyyear=('" + DropDownList2.SelectedItem.ToString() + "')", con);
       SqlDataAdapter da = new SqlDataAdapter(str, con);
       DataTable dt = new DataTable();
       da.Fill(dt);
       GridView1.DataSource = dt;
       GridView1.DataBind();



i am able to filter the data but i what i am not able to do is how to join two tables together and use them with dropdownlist condition

解决方案

see the syntax of INNER JOIN

SELECT ft.col1, ft.col2, st.col1, st.col2
FROM FristTable ft
INNER JOIN SecondTable st
ON ft.colname = st.colname



in your query there is no ON keyword...


correct the "inner join" query use "on" keyword to connect both tables
example:

select * FROM monthly AS m inner join yearly y on  m.pcode=y.pcode where m.pcode=('" + DropDownList1.SelectedItem.ToString() + "' ) AND y.fyyear=('" + DropDownList2.SelectedItem.ToString() + "'


这篇关于如何连接两个表并使用2个下拉列表过滤结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆