asp怎么多表查询

来源:百度知道 编辑:UC知道 时间:2024/06/07 22:49:23
sql="select * from products where anclassid="&anclassid&" order by adddate desc"
我现的工作需要同时查询另一个表“bsort”我像下面这样的写是错的
sql="select * from products,bsort where anclassid="&anclassid&" order by adddate desc"
请大吓指教一下

跨表查询叫内连接,可以这样 表名.列名

sql="select products.*,bsort.* from products,bsort where products.anclassid="&anclassid&" and products.userid=bsort.userid order by products.adddate desc"

上边只是个例子,如果你想用跨库查询用:数据库名.dbo.表名.列名

关键是你的SQL语句。
假设你的products里有一个ID字段,是整型的。在bsort里也有一个ID字段,也是整型的,并且二者是相同的(这个意思要是不明白我就不太好解释了。)

那么你的语句可以是
sql="select * from products join bsort on products.id=bsort.id where anclassid="&anclassid&" order by adddate desc"

假设你的products里有一个ID字段,是整型的。在bsort里也有一个ID字段,也是整型的.
sql="select * from products as a,bsort as b where a.id=b.id and anclassid="&anclassid&" order by adddate desc"