【问题标题】:SQLite table with AS SELECT带有 AS SELECT 的 SQLite 表
【发布时间】:2022-01-02 19:48:47
【问题描述】:

我有一个名为航班的表,其中包含与航班相关的所有信息,我有一个名为 users 的表。

我想创建一个名为 orders 的新表,在这个表中我想添加用户表中的用户名和航班表中的某些航班信息。

问题是我还想在我的订单表中有一个名为 orderID 的列。

我的问题是如何在选择查询中添加一列

【问题讨论】:

  • 听起来很奇怪。数据库设计应该是固定的,而不是即时生成的。你想在这里做什么?
  • 我正在尝试创建一个名为 order 的表,其中有一个名为 orderID 的列,我想将另一个表上的列添加到我的 order 表中

标签: sql sqlite


【解决方案1】:
SELECT *, an_expression AS another_column FROM the_table_or_subquery .... ; 
  • an_expression 是一个expression,它产生一个值
  • the_table_or_subqueryanother_column 是描述性的而不是实际的,请进行相应的更改。
  • 新列可能是第一个,例如SELECT an_expression AS another_column,* FROM the_table_or_subquery;

你能举个例子更好地理解

考虑到您提供的详细信息很少,这里是创建新表并从任何地方插入一些数据以及从另一个表(航班)插入其他数据的示例:-

DROP TABLE IF EXISTS flights;
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS `order`;
DROP TABLE IF EXISTS other_order_table;
CREATE TABLE IF NOT EXISTS flights (
    id INTEGER PRIMARY KEY, 
    flight_info TEXT
    )
;
CREATE TABLE IF NOT EXISTS users (
    userid INTEGER PRIMARY KEY, 
    user_name TEXT UNIQUE, 
    user_email TEXT UNIQUE
    )
;
INSERT OR IGNORE INTO flights (flight_info) 
    VALUES 
        ('Flight1'),
            ('Flight2'),
            ('Flight3')
;
INSERT OR IGNORE INTO users (user_name,user_email) 
    VALUES 
        ('Fred','fred@email'),
        ('Mary','mary@email'),
        ('Jane','jane@email')
;
DROP TABLE IF EXISTS `order`;

/* >>>>>>>>>> NOT A GOOD IDEAD <<<<<<<<<< due to
    A table created using CREATE TABLE AS has no PRIMARY KEY and no constraints of any kind. 
    The default value of each column is NULL. 
    The default collation sequence for each column of the new table is BINARY.
*/
CREATE TABLE IF NOT EXISTS `order` /* NOTE ORDER is a keyword so has to be enclosed - better to not call it order */
    AS SELECT *,null AS orderid /* The new column BUT see above, value will be null*/
    FROM flights;
    
SELECT * FROM `order`;

/* BETTER as can specify column attributes 
    however must insert elsewhere
*/
CREATE TABLE IF NOT EXISTS other_order_table (
    orderid INTEGER PRIMARY KEY, 
    order_added TEXT DEFAULT CURRENT_TIMESTAMP, 
    flight_id, 
    flight_info
    )
;
/*
    EXAMPLE 1 

    uses defaults for columns
    in the case of orderid as it's an alias of the rowid then autogenerated id
    in the case of order_added the current date and time in YYYY-MM-DD hh:mm:ss format
*/
INSERT INTO other_order_table (flight_id,flight_info) SELECT * FROM flights;
SELECT * FROM other_order_table;
DELETE FROM other_order_table;

/* EXAMPLE 2 */
INSERT INTO other_order_table
    SELECT 
        /* a random id will bne inserted into the first column (orderid) */
        abs(random()), 
        /* a random date up to 999 days in the past */
        datetime('now','-'||CAST(abs(random()) % 1000 AS INTEGER)||' days'),
        /* all the columnd from the flights tables */
        * 
    FROM flights
;
        
SELECT * FROM other_order_table;

/* Cleanup Ennvironment*/

DROP TABLE IF EXISTS flights;
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS `order`;
DROP TABLE IF EXISTS other_order_table;

3 个结果是:-

  • 航班表中的 2 列 + 新的 orderID 列设置为空
  • 警告请参阅上面关于列属性被剥离的评论

  • orderId 已生成
  • order_added 由于默认为 CURRENT_TIMESTAMP 而生成

新列 orderid 和 order_added 都使用返回随机合适值的表达式。

【讨论】:

  • 能否举个例子让大家更好理解
  • @kaylinnaidoo SELECT *,1 AS myNewColumn FROM sqlite_master;
  • @kaylinnaidoo 当然,如果您要充分描述您的需求,指定从什么到什么,那么可以提供更具体的答案。也许你应该重新阅读stackoverflow.com/help/how-to-ask
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-16
  • 1970-01-01
  • 2010-09-16
  • 1970-01-01
  • 2012-02-07
  • 1970-01-01
相关资源
最近更新 更多