Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Seeming Paradox

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
US
I'm trying to create a query that pulls certain matches from a table but I can't seem to work out how to get what I need and I'm not even sure I can explain it! I know it can be done because there is an existing site using the same database set but it was written in compiled C and I do not have the original uncompiled C file from which it was created.

The problem is that all the keywords are in a single column and I need the text values of QEValue where KeyID matches that submitted but only when the IN ('C1BA','C1CA','C1EA') values also exist in QEValue, which they never do in the same row as the KeyID. These particular IN values are actually KeyID 28 so, of course, if I use KeyID 28, then I get results but it is not a list of values where KeyID=109 or whatever is submitted by a form. In fact, the list of values is built up using several selectors so the IN value list grows from selector to selector in the form. The point is to act as a filter
for finding a list of "files" only when it matches.

The data is in sets by FileID with only a few columns but a hundred or so rows for each FileID but only a single KeyID and QEValue value for each set. I didn't design the database and have no control over it so it is what it is. I'm not sure how to apply the SQL with the KeyID as the input and to give a list of values that match the input and only when the same FileID has the IN values since it's not using the FileID.

Here is a select statement that doesn't work but it might give a hint of what I'm trying to do. If you can make sense of this, any ideas?

Code:
SELECT DISTINCT k.QEValue 
FROM dcs_keys k 
WHERE k.KeyID = 109 
AND k.QEValue <> ''
AND k.QEValue IN 
(SELECT QEValue FROM dcs_keys WHERE QEValue 
IN ('C1BA','C1CA','C1EA'))
ORDER BY k.QEValue
 
Having said all that, I think this should work although it seems to stall the server:

Code:
SELECT DISTINCT QEValue 
FROM dcs_keys 
WHERE FileID IN (SELECT FileID 
FROM dcs_keys 
WHERE KeyID = 109)
AND QEValue IN ('C1BA','C1CA','C1EA')
GROUP BY QEValue 
ORDER BY QEValue
 
I think that posting the question helped me work it out so I know I'm answering myself but this seems to be the answer. When I stall but maybe that is something I already did that hasn't yet freed up as it runs quickly in PHPMyAdmin:

Code:
SELECT DISTINCT QEValue 
FROM dcs_keys 
WHERE FileID IN (
SELECT FileID 
FROM dcs_keys 
WHERE QEValue IN ('C1BA','C1CA','C1EA')
) 
AND KeyID = 109  
GROUP BY QEValue 
ORDER BY QEValue
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top