我可以在SQLite中创建计算列吗? [英] Can I create computed columns in SQLite?
问题描述
例如(如果可能)创建一个名为 Car_Model
的表的语法是什么(如果可能),该表具有表的外键Car_Make
,并给 Car_Make
列,该列是存在的 Car_Models
的数量 Car_Make
。
What would be the syntax (if it's possible), for example, to create a table called Car_Model
that has a foreign key to a table Car_Make
, and give Car_Make
a column which is the number of Car_Models
that exist of that Car_Make
.
(如果这看起来很琐碎或类似家庭作业,那是因为我只是在家玩一些Python试图重现我在工作中遇到的问题。我们在工作中使用MS-SQL。)
(If this seems trivial or homework-like it's because I am just playing with some python at home trying to recreate a problem I was having at work. We use MS-SQL at work.)
推荐答案
更新:从版本开始 3.31.0 (于2020-01-22发行), SQLite支持计算列,因此以下答案适用于3.31.0之前的版本
Update: As of version 3.31.0 (released on 2020-01-22), SQLite supports computed columns so the answer below applies to versions prior to 3.31.0
SQLite不支持计算列。
SQLite doesn't supported computed columns.
但是,您的问题可以通过相对简单的SQL查询来解决,然后可以创建视图以使其看起来像带有额外计算的表d列。
However your problem can be solved with a relatively simple SQL Query, you can then create a view to make it appear like a table with the extra computed columns.
SELECT Car_Make.Name, Count(*) AS Count
FROM Car_Make, Car_Model
WHERE Car_Make.Id = Car_Model.Make
GROUP BY Car_Make.Name
这应返回类似于以下内容的表
This should return a table similar to the following
Name Count
---- -----
Nissan 5
Toyota 20
Ford 10
这篇关于我可以在SQLite中创建计算列吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!