我有三种类型的数据源。它们具有相似的结构,但有所不同。例如,类型A包含名为grade的列,但类型C不包含。在执行sql选择时,我只想编写一个查询(因为要选择的项目很多,而且大多数都相同)。这意味着对于C型数据源,将没有等级。在这种情况下,如何跳过此特定选择。
我的查询的说明如下:
SELECT
ID,
SUM(Addable) AS addable_sum,
MAX(Date) AS max_date,
GROUP_CONCAT(Char, '_') AS Char
AVG(grade) AS grade --------------> this is what I want to skip when dealing with type C data since there is not such thing in it
FROM TypeCTable ---------------> such table does not have grade column
GROUP BY ID
答案 0 :(得分:1)
是否需要联合选择,例如:
SELECT
ID,
SUM(Addable) AS addable_sum,
MAX(Date) AS max_date,
GROUP_CONCAT(Char, '_') AS Char
AVG(grade) AS grade
FROM TypeATable
GROUP BY ID
UNION ALL
SELECT
ID,
SUM(Addable) AS addable_sum,
MAX(Date) AS max_date,
GROUP_CONCAT(Char, '_') AS Char
NULL
FROM TypeCTable
GROUP BY ID
或使用UNION获得不同的结果