Solved

mysql query giving too many results

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

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Creating and Managing Databases with phpMyAdmin in cPanel.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

760 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

19 Experts available now in Live!

Get 1:1 Help Now