Solved

mysql query giving too many results

Posted on 2010-09-11
4
390 Views
Last Modified: 2012-06-27
I have a table automobiles with 7 results which contains vehicle info including the VIN
I have CBB table that contains the description of a vehicle I have joined the vin(the first 8 characters) in the automobiles table to the vin in the auto descritions table (cbb) because there are many cars made when comparing the first 8 characters of the vin im getting 30 records instead of 7, how do I make the query concentrate on the automobiles table rather then the cbb table... my present query is attached
SELECT a.automobileId, a.stockNumber, a.year, ap.photoName, c.data7, c.data8, c.data10, c.vin, t.transmissionType FROM automobiles AS a LEFT JOIN automobilePhotos AS ap ON a.automobileId=ap.autoId STRAIGHT_JOIN cbb AS c ON SUBSTRING(a.vin,1,8)=c.vin LEFT JOIN transmissions AS t ON a.transmissionId=t.transmissionId WHERE ap.isActive='true' AND ap.isDefault='true'

Open in new window

0
Comment
Question by:prowebinteractiveinc
  • 2
4 Comments
 
LVL 6

Expert Comment

by:DalHorinek
ID: 33653556
Use left join instead of STRAIGHT_JOIN

SELECT a.automobileId, a.stockNumber, a.year, ap.photoName, c.data7, c.data8, c.data10, c.vin, t.transmissionType FROM automobiles AS a LEFT JOIN automobilePhotos AS ap ON a.automobileId=ap.autoId LEFT JOIN cbb AS c ON SUBSTRING(a.vin,1,8)=c.vin LEFT JOIN transmissions AS t ON a.transmissionId=t.transmissionId WHERE ap.isActive='true' AND ap.isDefault='true'
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 33653571
I'm not a sql guru so your query seems to be too mutch complex: if you want you could try this

"SELECT automobiles.automobileId, automobiles.stockNumber, automobiles.year,
        automobilesPhptos.photoName,
        cbb.data7, cbb.data8, cbb.data10, cbb.vin, transmissions.transmissionType
        FROM automobiles, automobilesPhptos, transmissions
        WHEREautomobiles.automobileId=automobilesPhotos.autoId
        AND SUBSTRING(automobiles.vin,1,8)=cbb.vin
        AND automobiles.transmissionId=transmissions.transmissionId
        AND automobilesPhotos.isActive='true'
        AND automobilesPhotos.isDefault='true'":

Then you can try to add alias and join.

Hope this helps
0
 
LVL 6

Accepted Solution

by:
DalHorinek earned 500 total points
ID: 33653588
Also you should GROUP BY vin will give you only those records from automobiles (7) and assig to it a record from cbb (for each vin the first one found) ...

I don't know, what you want to display, if there are more records that matches vin code it cbb table.
0
 

Author Comment

by:prowebinteractiveinc
ID: 33653740
I already used LEFT JOIN the group by WORKED for now... I dont know if down the line when I add to my query if it will still work.. awarding points to DalHorinek
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

809 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