Solved

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

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
update joined tables 2 46
RDBMS and No sql database 4 58
MySqlDump not dumping triggers 1 30
Need Wordpress help to link from outside to image item number. 6 36
Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 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