Solved

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

Posted on 2004-08-09
5
741 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
ID: 11758571
>> 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
ID: 11763571
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
ID: 11764102
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
ID: 11764567
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
ID: 11980989
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Fore-Foreword Today (2016) Maxmind has a new approach to the distribution of its data sets.  This article may be obsolete.  Instead of using the examples here, have a look at the MaxMind API (https://www.maxmind.com/en/geolite2-developer-package). …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

821 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