在Transact-SQL中将TOP关键字应用于INTERSECTion

时间:2019-04-02 20:43:51

标签: sql sql-server tsql

我正在一个需要很多INTERSECTION并且使用一个相当大的数据库的项目中工作,所以我很希望能够对我的查询应用TOP来使事情变得不太慢。

问题是,我知道您可以做类似的事情(伪代码y,但我希望它是可以理解的):

db.collection(ns_status)
.where("state", "==", "online")
.onSnapshot(function(snapshot) {

    /*
    Return the promise so that resolved ns_match array can be used externally
    */
    return Promise.all(snapshot.docs.map((userSnapshot) => {
        /* 
        For each document in docs, map each to a promise. Once all of
        these promises are resolved, Promise.all() will pass the array
        of documents to the next then() handler 
        */
        return db.collection(ns_profile).doc(userSnapshot.id).get();
    }))
    .then(users => {
        /*
        We now have a list of user documents in users. It seems like 
        the filter method is better suited to what you're wanting, so
        filter user documents based on the condition that
        spoken_language === learning_language
        */
        return users.filter(doc => {
            const spoken_language = doc.data().spoken_language;
            return learning_language === spoken_language;
        });
    })
    .then(ns_match => {
        /* 
        We now recieve the list of filtered user documents where the
        language ns_match applies for all items in ns_match
        */
        console.log(ns_match);

        /* Return this filtered array for use outside of this call */
        return ns_match;
    })
})

但是

您能以某种方式在这些方面做些什么吗?

const UserModel = mongoose.model('User', new Schema({ ... }, { collection: 'User' }));

1 个答案:

答案 0 :(得分:2)

您可以将其编写为:

SELECT TOP 50 * from (SELECT * FROM A INTERSECT SELECT * FROM B) x; GO