Solved

SQL question-- Alternatives to IN statement (nested SQL)

Posted on 2004-08-09
5
736 Views
Last Modified: 2006-11-17
OK, in the absence of a successful mySQL 4.1 installation, I need to know some alternatives to using nested SQL statements.

here's the SQL I have now that is symptomatic of what I need to change:

SELECT field, field, field, field FROM tblcomp WHERE compID IN (SELECT compID_Inv FROM tblinv WHERE mfgID = 56;) ORDER BY CompanyName ASC;

tblcomp = company table
tbl inv = Inventory table

Each company could potentially own SEVERAL machines (held in the tblinv table), but all I WANT is ONE record for each company.  But when I use distinct keyword or DISTINCTROW or a LEFT join I still get multiple company records-- In the above example, it is grabbing a list of all companies that own a machine from a specific manufacturer (mfgID).  The IN statement works great-- but in mySQL it can't handle the nested query.  

Does ANYONE know a way to get the list either with a rewritten single SQL select statement or using a HEAP table?

Thanks!!

norm
0
Comment
Question by:normenclature
5 Comments
 
LVL 32

Expert Comment

by:ldbkutty
Comment Utility
>> OK, in the absence of a successful mySQL 4.1 installation,
wasn't PHPMyAdmin helped you ?

if i understood correctly,

>> The IN statement works great-- but in mySQL it can't handle the nested query.
IN Query supports SubQueries.

>> but all I WANT is ONE record for each company.

Give    LIMIT 1    at the end of the Query.
0
 
LVL 1

Accepted Solution

by:
burritoboy1227 earned 500 total points
Comment Utility
i had the same problem just the other day.  even after scouring throught the mysql manuals, i wasn't able to find a single SQL query that would solve this problem (i'm using mysql 4.0)...  so this is what i did.

1. create a temporary table with all the fields you need to select, and make compID the primary key.
2. do a replace into query, i.e.

replace into temp select tblcomp.compID, field, field, field from tblcomp, tblinv where tblcomp.compID=tblinv.compID_Inv AND tblinv.mfgID=56;

mysql will automatically blast all the duplicates, and whatever is left over in your temporary table should be the data you're looking for.
0
 
LVL 14

Expert Comment

by:psadac
Comment Utility
try this :

SELECT field, field, field, field
FROM tblcomp AS c INNER JOIN tblinv AS i ON c.compID = i.compID_Inv
WHERE i.mfgID = 56
GROUP BY i.compID_Inv
ORDER BY c.CompanyName;
0
 
LVL 1

Author Comment

by:normenclature
Comment Utility
thanks!

I decided instead of trying to figure out how to load 4.1 on top of XAMPP, I'd just reconfigure the SQL of the existing.  It really is only about 5 different SQL calls that I have to re-write.  Not a biggie.

I appreciate it--  I figured I'd have to do a temp table, but wasn't sure if there was something out there that someone else knew that I didn't.

thanks again.
0
 

Expert Comment

by:sajithnair
Comment Utility
This will solve your problem. Please try and let me know.

SELECT c.field, c.field, c.field, c.field FROM tblcomp c, tblinv i WHERE c.compID = i.compID_Inv
AND i.mfgID = 56 ORDER BY c.CompanyName ASC

Regards
Sajith
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now