简体   繁体   中英

How to see all data from one table and filtered on another, when using a join query (oracle sql)

Once I execute split, im hoping to see the following 3 things:

  1. The customer record from CUSTOMERS
  2. All customer transaction rows from TRANSACTIONS
  3. Items purchased made during transactions 5 and 6 from PURCHASES

My query below gets most of this right except for point 2, as it only returns transactions 5 and 6 instead of the full list. What can I change?

SELECT * FROM customers c
INNER JOIN transactions t ON c.custid = t.custid
INNER JOIN purchaces p ON t.transid = p.transid
WHERE c.customer = 1234 AND t.trans_num IN (5,6)
ORDER BY t.trans_num

You are explicitely filtering for records 5 and 6 in your where statement. You can LEFT JOIN the purchases table using that condition. You will get all transactions, but the purchases columns will be NULL if they don't belong to transactions 5 or 6.

SELECT * 
FROM customers c
INNER JOIN transactions t 
  ON c.custid = t.custid
LEFT JOIN purchaces p 
  ON t.transid = p.transid 
  AND t.trans_num IN (5,6)
WHERE c.customer = 1234 
ORDER BY t.trans_num

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM