Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
SQL Select with Subquery confusion
Message
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00053103
Message ID:
00053528
Views:
27
>This is from the Help file:
>
>"When the filter condition includes EXISTS, the filter condition evaluates to true (.T.) unless the subquery evaluates to the empty set."
>
>So, the query is something like:
>
>SELECT ... WHERE EXISTS (SELECT subquery...)
>
>If the subquery evaluates to a non empty result, the condition is true. If the result is empty, the condition is false.
>
>In your case:
>
>SELECT * FROM ALPHA_X WHERE NOT EXISTS( ;
> SELECT * FROM ALPHA_X WHERE stype="WL " AND progtype="NP") ;
> INTO CURSOR ALPHATMP
>
>The subquery:
>
>SELECT * FROM ALPHA_X WHERE stype="WL " AND progtype="NP"
>
>returned more than 5000 records, so it was not empty. Also, the subquery didn't depend on the main query, so, you had the same subquery result for each record in alpha_x. So, the EXISTS evaluated to true for all records in your table. But you used NOT EXISTS, so, the condition evaluated to false for all records in the table.
>
>So, the problem is not with adding table aliases, but with the logic of the composed SELECT.
>
>I wanted to clarify this problem, because, after this thread, the conclusion could be that VFP has problems when using the same table in the main and in the subquery, or with aliases. I already heard about this type of problems from many FoxPro programmers, but I couldn't find a single case to prove it. Although, the SELECT problems (most of them unreal) are used a lot as a "valid" reason to not use VFP. And I am a little tired to hear again about cases like that. :(

Ohhhhhhh.... I see now, Thank You Vlad! So the answer to my original question "What did I do wrong" is that I did NOT setup the subquery to depend on the main query. Even though I am only using a single table, I STILL have to setup a JOIN condition to tie the main query to the subquery? So when I did get this to work with the following:

SELECT * FROM ALPHA_X A WHERE NOT EXISTS( ;
SELECT * FROM ALPHA_X B WHERE ;
a.handle=b.handle AND a.ref_num=b.ref_num ; &&This missing was my problem.
AND b.stype<>"WL " AND b.progtype<>"NP") ;
INTO CURSOR ALPHATMP

I went back through the documentation too, and I think I understand this now. It seems when a SELECT statement contains a Subquery, the subquery is actually run for each record in the main query? Therefore creating the need to setup a Join condition to tie the subquery to each record in the main query? Am I seeing the light here Vlad or just more confused? :)
Roxanne M. Seibert
Independent Consultant, VFP MCP

Code Monkey Like Fritos
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform