Solved

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

Posted on 2004-08-09
5
742 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to count in a table in php 22 57
MySQL Persistent Connections 10 50
Add a date range function to PDF Generation 9 36
Export Data from MySql Using PHP 16 62
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…
Creating and Managing Databases with phpMyAdmin in cPanel.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

738 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