代码语言:javascript复制
SELECT
*
FROM
`user`
WHERE
`user`.id IN (
SELECT
`order`.user_id
FROM
`order`
)
当前语句等价于
代码语言:javascript复制List resultSet=[];
Array A=(select * from A);
Array B=(select id from B);
for(int i=0;i<A.length;i ) {
for(int j=0;j<B.length;j ) {
if(A[i].id==B[j].id) {
resultSet.add(A[i]);
break;
}
} }
return resultSet;
可以发现查询的次数取决于两张表的行数,时间复杂度为0(n2) 结论:in()适合B表比A表数据小的情况
代码语言:javascript复制SELECT
`user`.*
FROM
`user`
WHERE
EXISTS (
SELECT
`order`.user_id
FROM
`order`
WHERE
`user`.id = `order`.user_id
)
当前语句等价于
代码语言:javascript复制List resultSet[];
Array A = (SELECT 'user.*' FROM 'user');
for(int i = 0; i < A.length(); i){
if(EXIST(A[i])){
resultSet.add(A[i]);
}
<=> SELECT 'order.user_id' FROM 'order' WHERE 'user.id' = 'order.user_id';
}
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名,转载请标明出处