Select id,comp1 from table1,table2 where comp1='%"&Request("keyword")&"%' order by id desc"
但是运行程序时不能列出数据,请问这个数据怎么写?
...全文
1817打赏收藏
怎么写查询二个表的SQL语句?
数据库里有两张表,想在输入一个查询关健词后,在两个表里查出记录,如: 表1(table1)里有字段:id,comp1; 表2(table2)里也有字段id,comp1; 我想在SQL两个表一起查询: Select id,comp1 from table1,table2 where comp1='%"&Request("keyword")&"%' order by id desc" 但是运行程序时不能列出数据,请问这个数据怎么写?
Select table1.id,table2.comp1 from table1,table2 where comp1='%"&Request("keyword")&"%' and table1.id=table2.id order by table1.id desc"
既然都有字段id,comp1,那你就得给你的字段前加上表名,
不然不知道你要查那个表名,
还要就是要找多个表的关系,比如说table1.id=table2.id,不加这句,会查出count(*)from table1 乘 count(*)from table2条记录
你用的是“=”,应该用Like才是啊。
Select id,comp1 from table1,table2 where comp1 like '%"&Request("keyword")&"%' order by id desc"
另我写了另一句。
Select * from
(
Select id,comp1 from table1 where comp1 like '%"&Request("keyword")&"%'"
Union
Select id,comp1 from table2 where comp1 like '%"&Request("keyword")&"%'"
) A
order by id desc