选择具有虚拟值的虚拟列,然后在SQL中搜索虚拟列?

时间:2020-10-13 14:54:02

标签: mysql

我有两个需要加入的桌子

Table1
col1   col2   col3 
------------------
 1      A      20 
 2      B      10
 3      C      30
 4      D      40

Table2
col1   col2
-----------
 1      A  
 2      B  
 3      C  
 4      D  

我必须使两个表都具有UNION。由于 table2 没有col3,因此我在查询中添加了dummmy列。

我需要在两个表中都针对 col3

执行搜索操作

我尝试过,但是它抛出未知列异常。

(SELECT col1, col2, col3 from table1 where col3 = "10")
 UNION
(SELECT col1, col2, "10" as col3 from table2 where col3 = "10")

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:1)

您可以随后直接查询UNIONed数据:

private setupLogging() {
    const morganFactory = (config?: morgan.Options<Request, Response>) => {
      this.debug('Morgan configuration', config);
      return morgan('combined', config);
    };

    const defaultConfig: morgan.Options<Request, Response> = {};

    this.expressMiddleware(morganFactory, defaultConfig, {
      injectConfiguration: 'watch',
      key: 'middleware.morgan',
    });
}

答案 1 :(得分:0)

表2中不需要WHERE子句:

SELECT col1, col2, col3 FROM Table1 WHERE col3 = '10'
UNION 
SELECT col1, col2, '10' FROM Table2

请参见demo
结果:

> col1 | col2 | col3
> ---: | :--- | :---
>    2 | B    | 10  
>    1 | A    | 10  
>    3 | C    | 10  
>    4 | D    | 10 
相关问题