日期:2014-05-17  浏览次数:20433 次

aspx中多条件跨表查询
在做一个查询页面,里面有5个条件复合查询,其中一个条件要查询到另外一个表,想请教下各位该怎么写。谢谢


if (obj.name != null && obj.name != "")
   {
       Where += " and pt.name like '" + obj.name + "%'";
   }
if (obj.piz != null && obj.piz != "")
    {
       Where += " and pt.piz like '" + obj.piz + "%'";
     }
if (obj.wer != null && obj.wer != "")
     {
        Where += " and pt.wer like '" + obj.wer + "%'";
     }

//这里需要查询另外一个表,两者之间的关联是Cus.id,这样写是不对的,应该怎么表述才对呢

if (obj.telephone != null && obj.telephone !="")
   {
       Where += "and linkman.phone_home like '" + obj.telephone + "%' or linkman.phone_office like '" + obj.telephone + "%' (select * from linkman)";
    }
Sql = " pt.*,";

------解决方案--------------------
string sql = "select * from (select xx,xx,xx from table) t1"
if(condition)
{
  //
  sql+=",t2 where t1.id = t2.id and t2.xxx=....";
}
------解决方案--------------------
其实是,两个表关联之后所产生的数据集进行搜索。

search related:
http://www.cnblogs.com/insus/tag/Search/