Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

mysql query giving too many results

Posted on 2010-09-11
4
Medium Priority
?
395 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

610 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