I have tables with the following structure:
Model model_seq_num boxlable_model_desc partnumber agencypower_id1 agencypower_id2 Agencypower Agencypower_id Agencypower_desc
The query I wrote:
select m.model_seq_number, m.boxlabel_model_desc, m.partnumber, ap.agencypower_desc from model m, agencypower ap where m.agencypower_id1 = ap.agencypower_id
This query would give me all I want, except for the agencypower_desc corresponding to agencypower_id2. How do I modify this query to get that result as well? Any help is highly appreciated.
You just need to have two "copies" of the lookup table in the query. Use table alias names to distinguish them, and column alias names to distinguish the columns in the result set.
select m.model_seq_number , m.boxlabel_model_desc , m.partnumber , ap1.agencypower_desc as agencypower1 , ap2.agencypower_desc as agencypower2 from model m inner join agencypower ap1 on m.agencypower_id1 = ap1.agencypower_id inner join agencypower ap2 on m.agencypower_id2 = ap2.agencypower_id
For More Information
- Dozens more answers to tough SQL questions from Rudy Limeback.
- The Best SQL Web Links: tips, tutorials, scripts, and more.
- Have an SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
- Ask your technical SQL questions -- or help out your peers by answering them -- in our live discussion forums.
- Ask the Experts yourself: Our SQL, database design, SQL Server, DB2, object-oriented and data warehousing gurus are waiting to answer your toughest questions.
Dig Deeper on Oracle and SQL
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.