MySQL SQL Syntax question multiple table query

I have two tables 'Residential' and 'property_coordinates' that feature both feature a sMLSNumber field and the Residential table has a sSubdivision field.  I am trying to select all data where the sSubdivision equals Pines in table Residential and the sMLSNumber matches in both tables.  What am I doing wrong?  Can someone edit my syntax to work properly?
SELECT * FROM Residential, property_coordinates
	WHERE sSubdivision='Pines'
	AND sMLSNumber=sMLSNumber"

Open in new window

pda4meAsked:
Who is Participating?
 
NelliosConnect With a Mentor Commented:
The problem lies in the fact that mysql server doesn't know to which tables column you are refering by sMLSNumber since it exists in both tables that participate in the query:

You can try one of the snipets bellow.


/*1st Approach*/
SELECT * FROM Residential, property_coordinates
        WHERE sSubdivision='Pines'
        AND Residential.sMLSNumber=property_coordinates.sMLSNumber
 
/*2nd Approach*/
select * from Residential
inner join property_coordinates on property_coordinates.sMLSNumber=Residential.sMLSNumber

Open in new window

0
 
pda4meAuthor Commented:
I fixed a typo " and am now getting this error FYI, how do I work around:

#1052 - Column 'sMLSNumber' in where clause is ambiguous
0
 
pda4meAuthor Commented:
Is there a performance increase in one versus the other?
0
 
virmaiorConnect With a Mentor Commented:
the two queries will probably evaluate the same since MySQL optimizes the query that you send it.  So properly speaking these are not approaches to the query so much as approaches to programming practices.

I personally always prefer the second syntax.  With the first syntax, it isn't clear whether the parts of the WHERE clause are clarifying which rows you want to retrieve (e.g. sSubdivision = 'Pines' ) and which parts are explaining how the data is related ( Residential.sMLSNumber=property_coordinates.sMLSNumber ).  In the second syntax, it is obvious, explicit, and required.
0
 
pda4meAuthor Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.