>  기사  >  백엔드 개발  >  将子查询改造成join连接查询

将子查询改造成join连接查询

WBOY
WBOY원래의
2016-06-06 20:40:082658검색

原来的子查询

SELECT COUNT(*) AS tp_count FROM sdb_b2c_orders WHERE pay_status='1' and createtime>1413533130 and area_code in (1030,1031,1032,1033) and member_id in (select member_id from sdb_invite_invite where in_member_id=14273 or in_member_id=13742 or in_member_id=14299) LIMIT 1

执行结果是22

使用连接查询代替上面的子查询

SELECT COUNT(*) AS tp_count FROM sdb_b2c_orders AS bo INNER JOIN sdb_invite_invite AS ii ON bo.member_id=ii.in_member_id WHERE bo.pay_status='1' AND bo.area_code IN (1030,1031,1032,1033) AND bo.createtime>1413533130 AND ii.in_member_id IN (14273,13742,14299)

执行结果却是131

回复内容:

原来的子查询

SELECT COUNT(*) AS tp_count FROM sdb_b2c_orders WHERE pay_status='1' and createtime>1413533130 and area_code in (1030,1031,1032,1033) and member_id in (select member_id from sdb_invite_invite where in_member_id=14273 or in_member_id=13742 or in_member_id=14299) LIMIT 1

执行结果是22

使用连接查询代替上面的子查询

SELECT COUNT(*) AS tp_count FROM sdb_b2c_orders AS bo INNER JOIN sdb_invite_invite AS ii ON bo.member_id=ii.in_member_id WHERE bo.pay_status='1' AND bo.area_code IN (1030,1031,1032,1033) AND bo.createtime>1413533130 AND ii.in_member_id IN (14273,13742,14299)

执行结果却是131

sdb_invite_invite.in_member_id记录有重复

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.