?
Solved

mysql query giving too many results

Posted on 2010-09-11
4
Medium Priority
?
394 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
[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
  • 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 2000 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

801 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