【发布时间】:2021-11-16 17:41:33
【问题描述】:
我是 sql 查询的新手。我有从这 3 个表中获取数据的基本查询,下面是查询,
select t.transction_id,pt.name,pa.date from transaction t, position_acc pa,position_trans pt where
t.transction_id=pt.transction_id and pt.postion_id=pa.postition_id
========================
现在我有一个要求,我必须从另一个表调用中获取数据,移动
这是我客户的条件。他们需要移动表中的所有数据,即使 position_trans 或 position_acc 中没有匹配的数据。
所以,这是我的客户的条件:
select t.transction_id ,m.movement_id from transction t,movement m where t.transction_id=m.movement_id and m.name="CASH"
如何将这种情况与我的基本查询结合使用。客户端需要来自移动的所有数据。如果匹配的列在 position_trans 或 position_acc 中没有条目,它们很好。他们将接受这些列中的列为空。
那么,我怎样才能将我的运动条件放入基本查询中。请帮助
======
select t.transction_id ,m.movement_id from transction t,movement m where t.transction_id=m.movement_id and m.name="CASH"
Record count =1500.
The count should be same after appending with base query.
【问题讨论】:
-
使用标准的 JOIN 语法。你想要 LEFT JOIN。
-
@Serg 在基本查询之后我应该在哪里使用左连接?从事务 t 中选择 t.transction_id,pt.name,pa.date, position_acc pa,position_trans pt where t.transction_id=pt。 transction_id 和 pt.postion_id=pa.postition_id LEFT 在 t.transction_id=m.movement_id 上加入运动 m,其中 m.name="CASH"。对吗??