有表1
pid pathurl
1 /upload/2011/04/05/20010405.jpg
2 /upload/2011/04/05/20010406.jpg
3 /upload/2011/04/05/20010407.jpg有表2
pid attr1
1 方法1
1 方法2
1 方法3
2 方法2
3 方法4
1 方法4
表1与表2是一对多的关系。我现在需要查询出
表2中attr1='方法2'的记录或查询出
带有方法1并且带有方法2的记录来。出来的记录格式需要是:
pid pathurl attr1
1 /upload/2011/04/05/20010405.jpg 方法2用group by和having是可以,但效率很低。
pid pathurl
1 /upload/2011/04/05/20010405.jpg
2 /upload/2011/04/05/20010406.jpg
3 /upload/2011/04/05/20010407.jpg有表2
pid attr1
1 方法1
1 方法2
1 方法3
2 方法2
3 方法4
1 方法4
表1与表2是一对多的关系。我现在需要查询出
表2中attr1='方法2'的记录或查询出
带有方法1并且带有方法2的记录来。出来的记录格式需要是:
pid pathurl attr1
1 /upload/2011/04/05/20010405.jpg 方法2用group by和having是可以,但效率很低。
from tb1 A
where A.pid in (
select pid
from tb2 B
where B.attr1='方法1' and exists(select 1 from tb2 C where B.pid=pid and attr1='方法2')
)
from 表2 a inner join 表2 b on a.pid=b.pid
inner join 表1 c on a.pid=c.pid
where a.attr1='方法1' and b.attr1='方法2'
这样肯定查不出记录的吧?因为attr1不可能等于1也等于2吧?
带有方法1并且带有方法2的记录来。SELECT * from (表2 a inner join 表2 b on a.pid=b.pid ) inner join 表1 c
on a.pid=c.pid
where a.attr1='方法1' and b.attr1='方法2'测试通过
FROM t1
INNER JOIN
t3 ON t3.stu_id = t1.stu_id
WHERE
t3.co_id = 1 AND t3.co_id = 5t1表
stu_id stu_name
1 aa
2 bb
3 cct3表
stu_id co_id
1 1
1 4
2 1
2 3
2 5我使用了你这个方法不行哦?呵。
SQL语句是:
SELECT t1.stu_id,t1.stu_name,t3.co_id#,t2.co_name
FROM t1
INNER JOIN
t3 ON t3.stu_id = t1.stu_id
WHERE
t3.co_id = 1 AND t3.co_id = 5