如何在sqlite3 python中返回交叉值

时间:2018-04-04 08:13:28

标签: python sqlite

我试图以JSON格式返回有多少演员参与英语电影,德语电影等的信息。

例如

我拍摄language_id并查看有多少部电影拥有这些语言。然后我检查有多少演员(actor_id)拥有这些电影的每种语言版本。

我想返回如下内容:

{"English" : 123, #(different actors (by `actor_id`)
 "German" : 2456, 
 ... etc
}

分贝

film_actor    film           language
----------    ------         ---------
 actor_id     film_id        language_id
 film_id      title          name
              language_id

我知道如何回复每个language_id有多少部电影,但我不知道如何加入,计算和返回更多数据。

@app.route('/langs')
def lang_list():
# query = '''
# SELECT name, COUNT(name) FROM language 
# JOIN film USING (language_id) 
# GROUP by name

# '''
query = '''
SELECT name from language
JOIN film_actor using (film_id) 
GROUP by name

'''
args = ()
db = get_db()
data = db.execute(query, args).fetchall()
return_json= []
for i in data:
return_json.extend(list(i))

return_json = dict(itertools.zip_longest(*[iter(return_json)] * 2, fillvalue=""))

return jsonify(return_json)

if __name__ == '__main__':
app.run(debug=True)

2 个答案:

答案 0 :(得分:0)

你可以得到这样一种语言的数量:

SELECT COUNT(*) FROM film_actor 
JOIN film USING (film_id) 
JOIN language USING (language_id) 
WHERE name = "English";

答案 1 :(得分:0)

var ad = ad || {};
ad.mvvm = ad.mvvm || {};

ad.mvvm.documents = (function (ad, ko, document) {
    "use strict";
    var
        showLoadingScreen = function () {
            $('.loadingScreen').show();
        },
        hideLoadingScreen = function () {
            $('.loadingScreen').fadeOut(250);
        },
        tabs =
        [
            {
                name: "Suppliers",
                innerModel: null,
                template: 'template-attachedDocs-supplier',
                model: function () {
                    if (!this.innerModel) {
                        ad.views.supplier.init({});

                        this.innerModel = ad.views.supplier;
                    }
                    return this.innerModel;
                },
                setMethods: function () {
                    ad.views.uploadDocuments.callbacks.uploadFile = ad.views.supplier.uploadFile;
                    ad.views.supplier.viewModel.callbacks.deleteButtonClicked = ad.views.supplier.deleteSupplierDocument;
                    ad.views.supplier.viewModel.callbacks.updateButtonClicked = ad.views.supplier.updateSupplierDocument;

                    ad.views.supplier.viewModel.callbacks.uploadButtonClicked = function () {
                        ad.views.uploadDocuments.open(ad.views.supplier.viewModel.selectedSupplier(), ad.views.uploadDocuments.properties.source.Supplier);
                        ad.views.uploadDocuments.setTitle('Supplier - ' + ad.views.supplier.viewModel.selectedSupplier());
                    };

                    ad.views.uploadDocuments.callbacks.close = ad.views.supplier.refresh;
                    ad.views.uploadDocuments.callbacks.loading = showLoadingScreen;
                    ad.views.uploadDocuments.callbacks.loadingComplete = hideLoadingScreen;
                }
            }, {
                name: "Accounts",

(etc)

}
    ],
        init = function () {

            ad.views.documents.init(tabs, bulktabs);

            ko.applyBindings(ad.views.documents.viewModel, document.getElementById("documentsView"));

            ad.views.uploadDocuments.init({
                popup: "#AttachDocsPopup"
            });
            ko.applyBindings(ad.views.uploadDocuments.viewModel, document.getElementsByClassName("attachDocuments-modal")[0]);

        };



    return {
        init: init
    };
})(ad, ko, document);

$(document).ready(function () {
    "use strict";
            $('.loadingScreen').fadeOut(250);
            return -1;
        }
    }

    ad.mvvm.documents.init();

    $(".tabs").tabs({
    });

    $('.loadingScreen').fadeOut(250);
});