日期:2014-05-19  浏览次数:20526 次

高手帮忙解释下 这个搜索语句的作用
string   str= "SELECT   *,(SELECT   name   FROM   businesssort   where   id   =   business.con_sort)   as   Con_sortname,(SELECT   name   FROM   businesssort2   where   id   =   business.con_xingzhi)   as   Con_sortname2,(SELECT   name   FROM   businesssort3   where   id   =   business.con_renshu)   as   Con_sortname3   FROM   business   order   by   id   desc ";


其中括号里的什么意思啊     一共是有3个表

------解决方案--------------------
括號裡面的就是三個子查詢。

改寫為如下方式更好些。

string str= "SELECT A.*, B.name as Con_sortname, C.name as Con_sortname2, D.name as Con_sortname3 FROM business A Inner Join businesssort B On A.con_sort = B.id Inner Join businesssort2 C On A.con_xingzhi = B.id Inner Join businesssort3 D On A.con_renshu = D.id order by A.id desc ";
------解决方案--------------------
SELECT *,(SELECT name FROM businesssort where id = business.con_sort) as Con_sortname,(SELECT name FROM businesssort2 where id = business.con_xingzhi) as Con_sortname2,(SELECT name FROM businesssort3 where id = business.con_renshu) as Con_sortname3 FROM business order by id desc
提取表business中按照id倒序排列的所有记录,同时提取businesssort表中,id等于表business的con_sort的name值,别名为Con_sortname,其他的同理;

也可以建立视图的