Solved

mysql query giving too many results

Posted on 2010-09-11
4
388 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

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…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now