将 UNION ALL 与另一张表一起使用时,保留一张表中的所有记录

Posted

技术标签:

【中文标题】将 UNION ALL 与另一张表一起使用时,保留一张表中的所有记录【英文标题】:Keep all records from one table when using UNION ALL with other table 【发布时间】:2014-06-30 14:24:50 【问题描述】:

我的两张表 - UserLogUserInspection 这样UserLog UNION ALL UserInspection

我想保留来自UserLog 的所有记录,但想保留来自UserInspection 的一些记录,其OWNER column owner 值相同。

插图:

String OWNER = "0394850935803";

ArrayList<HashMap<String, String>> list = 
         new ArrayList<HashMap<String,String>>();

HashMap<String, String> dataMap = null;

table_log = "SELECT Type_Log, Start AS SortDate, Location, 
             Remark, Odometer, NULL AS Start_odo, NULL AS owner 
             FROM UserLog WHERE deleted !=1 ";

table_insp = "select Type, Date, Location, Observation, NULL, 
              Start_odo, owner from UserInspection where deleted !=1 ";

final_query = table_log + " UNION ALL " + table_insp + " ORDER BY SortDate";

cur = dbAdapter.rawQuery(final_query, null);

cur.moveToFirst();

for (int j = 0; j < cur.getCount(); j++)

    /*Keep all the UserLog records but keep only*/
    /*those UserInspection records whose owner column value*/
    /*from cursor matches the OWNER String value*/
    if((cur.isNull(cur.getColumnIndex("owner"))) 
     || OWNER.equals(cur.getString(cur.getColumnIndex("owner"))))

      /*Code to get other column values*/
      dataMap = new HashMap<String, String>();
      dataMap.put("Location", location);
      list.add(dataMap);
    

但我在 ArrayList list 中什么也得不到,即使 UserLog 表有一些记录。

【问题讨论】:

我敢打赌你的错误来自你的 orderby 条款。你的第二个查询没有SortDate,所以它会抛出一个错误。 @paqogomez ORDER BY 可能只有一个UNION,所以它是正确的 【参考方案1】:

只需将附加条件放入 WHERE 子句中,则无需在代码中检查:

...
table_insp = "select Type, Date, Location, Observation, NULL, "+
             "Start_odo, owner from UserInspection "+
             "WHERE deleted !=1 "+
               "AND owner = ?";

final_query = table_log + " UNION ALL " + table_insp + " ORDER BY SortDate";
String[] parameters = new String[]  OWNER ;

cur = dbAdapter.rawQuery(final_query, parameters);
...

【讨论】:

我希望我能为这样一个简单的解决方案投票给你更多。谢谢先生。【参考方案2】:

如果您可以处理将第二个表的内容作为附加列,则使用left outer join

select *
from (SELECT Type_Log, Start AS SortDate, Location, 
             Remark, Odometer, NULL AS Start_odo, NULL AS owner 
      FROM UserLog
      WHERE deleted <> 1
     ) table_log left outer join
     (select Type, Date, Location, Observation, NULL, 
             Start_odo, owner
      from UserInspection
      where deleted <> 1 
     ) table_insp
     on table_log.owner = table_insp.owner;

如果你真的想要单独的行,你可以使用with:

with table_log as 
      SELECT Type_Log, Start AS SortDate, Location, 
             Remark, Odometer, NULL AS Start_odo, NULL AS owner 
      FROM UserLog
      WHERE deleted <> 1
    ),
     table_insp as (
      select Type, Date, Location, Observation, NULL, 
              Start_odo, owner
      from UserInspection ui
      where deleted <> 1 and
            exists (select 1 from table_log where ui.owner = table_log.owner)
     )
select *
from table_log
union all
select *
from table_insp;

【讨论】:

使用 LEFT OUTER JOIN 保留左表中的所有记录很好,但左表所有者和右表所有者之间没有连接,因此 where 子句 table_log.owner = table_insp.owner 永远不会为真。相反,我想使用OWNER 字符串值来匹配table_insp.owner。如何在您的查询中使用此字符串值。

以上是关于将 UNION ALL 与另一张表一起使用时,保留一张表中的所有记录的主要内容,如果未能解决你的问题,请参考以下文章

Excel:VLOOKUP 将一列与另一张表匹配并连接数据

unionall会锁表吗

mysql union和union all的区别

如何在 Apollo 中将一张表关联到另一张表

mysql 获取一张表中, 另一张表不存在的数据

数据库表关系相关案例