Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please help with a query

Status
Not open for further replies.

UBfoolin

Programmer
Nov 29, 2001
32
US
I can not figure out how to do this in MySQL 4.0.12.

The rule is, in English, if an account has a Code1 then it should not have a Code2. I need to find all of the T1.Code values that break this rule.

Table1 (T1) [50,000 accounts; Approx 20 codes per acct]
Account
Code

Table2 (T2) Has 200,000 records
Code1
Code2

I need to get all accounts where one of T1.Code matches a T2.Code1 value, and the same T1.Account has another T1.Code that matches a T2.Code2 value.

I specifically need the T1 record where T1.Code = T2.Code2. The final recordset will be only these records.

I have no restrictions on how to accomplish this. Temp tables are ok. Needs to be fairly quick though.

All help is greatly appreciated!
Thank you.
 
I think I may have been unclear in my request above. T2 can have a specific Code1 value several times with various Code2 values.

An example:
T1
Account Code
123 A
123 B
123 4
123 8

T2
Code1 Code2
B A
B Y
B 1
B 4

I want to end up with all records, for each account, where T1.Code(Value1)=T2.Code2 and T1.Code(Value2)=T2.Code1. In the example above, the result would be:
Acct=123, Code=4, Code1=B, Code2=4.

In other words, once a T1.Code match is made with T2.Code1, if there is another T1.Code match with T2.Code2, select the record.
Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top