Solved

MySQL SQL Syntax question multiple table query

Posted on 2008-06-18
5
240 Views
Last Modified: 2013-12-12
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

0
Comment
Question by:pda4me
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 

Author Comment

by:pda4me
ID: 21812295
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
 
LVL 10

Accepted Solution

by:
Nellios earned 400 total points
ID: 21812373
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
 

Author Comment

by:pda4me
ID: 21812562
Is there a performance increase in one versus the other?
0
 
LVL 20

Assisted Solution

by:virmaior
virmaior earned 100 total points
ID: 21812879
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
 

Author Closing Comment

by:pda4me
ID: 31468311
Thanks!
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

628 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