在 MySql 中,我可以在不使用联接和使用单个 where 声明的情况下从同一用户的两个单独表中选择数据吗?
示例: 表1
userSent | userId
10 | 1
表2
userNew | userId
15 | 1
sql 示例
(
select userSent from table1
union
select userNew from table2
) where userId = 1;
我实际上正在尝试从不同的表中获取同一用户的数据。尝试同时运行两个查询。
最佳答案
有可能,但我不推荐。
你可以这样做
select user from
(
select userSent as user,userId from table1
union all
select userNew,userId from table2
) tab
where userId = 1;
但请记住,您首先要完全读取两个表,然后将它们组合起来(甚至可能在数据较多时在临时表中)并过滤此表。 性能噩梦!不要偷懒,发起两个查询。或者更好的是,加入这些查询。
关于MySql 从两个表中选择数据,不使用 join,而是使用单个 where 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25155688/