Solved

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

Posted on 2004-08-09
5
743 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

705 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