SQL检查一对元组在另一个表中是否至少有一个匹配项

时间:2018-06-25 20:19:14

标签: sql db2

我有2个表:memberpurchase

会员可以属于多个俱乐部,并且可以通过俱乐部进行购买。因此,例如,成员A可以属于3个俱乐部,并且可以通过俱乐部A购买一件商品,并且可以通过俱乐部B购买一件商品。

会员和购买的会员均包含会员ID和俱乐部名称。

我如何确定会员是否从其所属的每个俱乐部购买了商品(无关紧要)?

编辑:这是一个更大的家庭作业问题的一部分,以查找尚未从其所在的每个俱乐部购买书籍的会员。我的想法是使用EXCEPT子句删除已从其每个俱乐部购买书籍的所有人。但我在这部分上遇到了困难。

1 个答案:

答案 0 :(得分:0)

您可以计算会员购买的不同俱乐部的数量,并将其与俱乐部总数进行比较:

    @Override
    public byte[] sign(InputStream content) throws IOException {

        // cannot be done private (interface)
        try {

            // Certificate chain is acquired at initialization
            List<Certificate> certList = new ArrayList<>();
            certList.addAll(Arrays.asList(certificateChain));
            Store<?> certs = new JcaCertStore(certList);
            org.bouncycastle.asn1.x509.Certificate cert = org.bouncycastle.asn1.x509.Certificate.getInstance(certificateChain[0].getEncoded());

            CMSSignedDataGenerator gen = new CMSSignedDataGenerator();

            //HSMSigner is the class that interacts with the network HSM to get the signature.
            HSMSigner signer = new HSMSigner();
            byte[] input = IOUtil.toByteArray(content);

            //This is the update over previous code.
            //Create a hash of the content and add it to the attribute map
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
            messageDigest.update(input);
            Attribute attr = new Attribute(CMSAttributes.messageDigest, new DERSet(new DEROctetString(messageDigest.digest())));
            ASN1EncodableVector v = new ASN1EncodableVector();
            v.add(attr);

            ContentSigner sha1Signer = new ContentSigner() {
                //This is to ensure that signature is created using the right data.
                ByteArrayOutputStream stream = new ByteArrayOutputStream();

                @Override
                public byte[] getSignature() {
                    //Calling HSM here instead, the stream is the AttributeMap
                    return Base64.getDecoder().decode(signer.getSignature(stream.toByteArray()));
                }
                //Perhaps called by BouncyCastle library to provide the content
                @Override
                public OutputStream getOutputStream() {
                    return stream;
                }

                @Override
                public AlgorithmIdentifier getAlgorithmIdentifier() {
                    return new DefaultSignatureAlgorithmIdentifierFinder().find("SHA256WITHRSAENCRYPTION");
                }
            };

            //As per mkl's comment, using the AttributeTable as an input where the table already has a Hashed value of the content.
            SignerInfoGeneratorBuilder builder = new SignerInfoGeneratorBuilder(new BcDigestCalculatorProvider())
                    .setSignedAttributeGenerator(new DefaultSignedAttributeTableGenerator(new AttributeTable(v)));

            gen.addSignerInfoGenerator(builder.build(sha1Signer, new X509CertificateHolder(cert)));

            gen.addCertificates(certs);
            CMSProcessableInputStream msg = new CMSProcessableInputStream(content);
            CMSSignedData cmsSignedData = gen.generate(msg, true);

            byte[] result = cmsSignedData.getEncoded();

            return result;

        } catch (GeneralSecurityException | CMSException | OperatorCreationException e) {
            throw new IOException(e);
        }

    }