作者appleboy46 (我是道明柏毅)
看板Database
标题[SQL ] 查询3个表格的语法
时间Thu Apr 5 22:03:22 2007
我先列出3个表格的栏位跟一些值
table1
class_id, class_title
1 name1
2 name2
table2
class_id, series_name
1 000001
1 000002
1 000003
2 001001
2 001002
2 001003
table3
group_id, group_name
1 000001
1 000002
1 000003
1 000004
2 001001
2 001002
2 001003
如果想要 table1 跟 table2 结合 只要下
sql语法一
Select * From table1 as t1, table2 as t2 where t1.class_id = t2.class_id &&
SUBSRTING(`series_name`,1,3) = '000'
如果想要 table1 跟 table3 结合 只要下
sql语法二
Select * From table1 as t1, table3 as t2 where t1.class_id = t3.group_id &&
SUBSRTING(`group_name`,1,3) = '000'
但是我想要用一句sql语法把上面2个sql语法取出来的资料,也就是
sql语法一取出3笔
sql语法二取出4笔
然後我想用一个sql语法可以取出上面7笔资料,我是这样写sql
Select * From table1 as t1, table2 as t2, table3 as t3
where (t1.class_id = t2.class_id && SUBSRTING(`series_name`,1,3) = '000') ||
(t1.class_id = t3.group_id && SUBSRTING(`group_name`,1,3) = '000')
但是这样取出来的不是我想要的,请问要怎麽下sql会比较好
谢谢大家喔 我想了一个下午 搞不太定
--
********************************************************
* Appleboy Blog *
*
http://blog.Wu-Boy.com/ *
* Appleboy Album *
*
http://www.wretch.cc/album/appleboy *
********************************************************
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 218.163.170.24