首页 > 其他分享 >join查询的on和where的区别

join查询的on和where的区别

时间:2022-12-02 21:33:53浏览次数:33  
标签:aRecord join name aRecords public 查询 where id String

import java.util.ArrayList;
import java.util.List;

class A {

public Integer id;

public String name;

}

class B {
public Integer id;

public String name;

}

public class C {
public Integer aId;
public String aName;
public Integer bId;
public String bName;

public C(Integer aId, String aName, Integer bId, String bName) {
    this.aId = aId;
    this.aName = aName;
    this.bId = bId;
    this.bName = bName;
}

/**
 * join on a.name=b.name and a.id>10
 * aRecords一定是主表,return结果就是join on condition的结果
 * 前期后后,这篇文章从2022/12/2 16:00开始到现在21:00,5个小时,发现用代码最好描述和理解。写出来之后,我挺强的(内心)
 * @param aRecords 主表数据
 * @param bRecords 从表数据
 * @return  on conditons 之后的join结果——中间表C
 */
public List<C> onConditionCartesianProduct(A[] aRecords, B[] bRecords) {
    List<C> cRecords = new ArrayList<>();
    for (int i = 0; i < aRecords.length; i++) {
        A aRecord = aRecords[i];
        boolean existsConditionReturnTrue = false;
        for (int j = 0; j < bRecords.length; j++) {
            B bRecord = bRecords[j];
            if (aRecords[i].name == bRecords[j].name && aRecords[i].id > 10) {
                existsConditionReturnTrue = true;
                C cRecord = new C(aRecord.id, aRecord.name, bRecord.id, bRecord.name);
                cRecords.add(cRecord);
                // 这里不进行break,笛卡尔积还会使用下一条bRecord继续和当前aRecord进行判断
            }
        }
        if (!existsConditionReturnTrue) {
            C cRecord = new C(aRecord.id, aRecord.name, null, null);
            cRecords.add(cRecord);
        }
    }
    return cRecords;
}

/**
 * where a.id != 10 and b.name != 'b'
 * @param onConditionResult
 * @return
 */
public void whereCondition(C[] onConditionResult) {
    for (int i = 0; i < onConditionResult.length; i++) {
        C c = onConditionResult[i];
        if (c.aId != 10 && c.bName != "b") {
            // 保留这条符合where condition的数据c
        } else {
            // 从onConditionResult移除这条不符合where condition的数据c
        }
    }
    // 最后,返回现在的onConditionResult
}

}

标签:aRecord,join,name,aRecords,public,查询,where,id,String
From: https://www.cnblogs.com/woyujiezhen/p/16945677.html

相关文章