If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > using LIKE with subqueries/JOIN (DB2)

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-29-04, 09:49
patlv23 patlv23 is offline
Registered User
 
Join Date: May 2002
Location: Philippines
Posts: 11
Question using LIKE with subqueries/JOIN (DB2)

Hi

Say I have these tables

Account
account_id char(9) ex. "ABC123456", "XYZ332211"
(ledger_id + sequence num)

Ledger
ledger_id char(3) ex. "ABC", "XYZ"
branch_id char(1) ex. "B", "C"

I'd like to get all accounts given a branch_id. This would be easy enough if account_id were broken into ledger_id and sequence_num. I suppose it would look something like this:

SELECT *
FROM Account
JOIN Ledger
ON Account.ledger_id = Ledger.ledger_id
WHERE branch_id = 'B'

or

SELECT *
FROM Account
WHERE Account.ledger_id = (SELECT ledger_id
FROM Ledger
WHERE branch_id = 'B')

Could there be a LIKE version? something like:

SELECT *
FROM Account
JOIN Ledger
ON Account.account_id LIKE Ledger.ledger_id CONCAT '%'
WHERE branch_id = 'B'

or like:

SELECT *
FROM Account
WHERE account_id LIKE (SELECT ledger_id CONCAT '%'
FROM Ledger
WHERE branch_id = 'B')

Thanks in advance!

IBM DB2 v8.1.0.36
Windows 2000
Reply With Quote
  #2 (permalink)  
Old 07-01-04, 08:32
dmmac dmmac is offline
Registered User
 
Join Date: Aug 2003
Location: Massachusetts, USA
Posts: 106
SELECT *
FROM Account
JOIN Ledger ON SUBSTR(Account.account_id,1,3) = Ledger.ledger_id
WHERE branch_id = 'B'
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On