Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
WHERE clause with JOIN syntax
Message
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01080471
Message ID:
01080491
Views:
15
>>>I seem to keep getting syntax errors if I try to use WHERE and JOIN ON in the same SQL query. Can you use a where clause in an SQL statement that is written as a JOIN, or do all the "WHERE" conditions for both tables have to be in the ON clause?
>>>
>>>Does anybody have a "personal favorite" suggestion for an advanced SQL tutorial / reference?
>>>
>>>Thanks very much.
>>
>>I routinely use JOIN ON syntax to relate tables with a following WHERE clause to filter the results using fields from one or more of the joined tables.
>>
>>The best thing I've seen is "Taming Visual FoxPro's SQL" by Tamar Granor with Della Martin. It's available as an Ebook from Henzenwerke Publishing, http://www.hentzenwerke.com/.
>>
>>Regards,
>
>Thanks very much. I wonder if it's the positioning I'm doing wrong. A statement like:
>
select a.field1, a.field2, b.fieldfromb ;
>where a.field1="X" ;
>from table1 a ;
>inner join table2 b on b.somefield=a.somefield
>gives me an error, but if I change the statement to
>
select a.field1, a.field2, b.fieldfromb ;
>from table1 a ;
>inner join table2 b on b.somefield=a.somefield and a.field1="X"
then the error goes away. ????

The easiest solution is to remember that the WHERE clause comes next to the last in the Syntax (i.e, after the selected tables and joins). From your sample above you have it before you tell VFP what tables you're even using. Rewriting your problematic one as :
select a.field1, a.field2, b.fieldfromb ;
from table1 a ;
inner join table2 b on b.somefield=a.somefield;
where a.field1="X"
should work with no errors.

Richard
State of Florida, DCF
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform