Solved

MYSQL select statement, 2 fields with same name in different table

Posted on 2008-06-14
4
1,167 Views
Last Modified: 2013-12-13
I have 2 tables:
Car and Car_Make

In Car I have 2 fields, car_make and car_model, both are int.
Example:
Car_Make, Car_Model
1,20

In Car_Make I have 2 fields, make_id and make_name
Example:
make_id, make_name
1,Buick
20,Skylark

I want to be able to use my select statement to select all the cars in the Car table, however I need the make and model name from the car_make table.

I don't know how to write the statement, since the make_name field has both of the fields I need.

Here is a sample select to start with, that just selects the int values:
$query1  = "SELECT car_model, car_make, car_year, car_price, car_mileage, car_avail FROM site_cars ORDER BY $sortby";
0
Comment
Question by:baumli1
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
Xorlev earned 125 total points
ID: 21787275
What I recommend you do is have 3 tables
cars:
entry_id, make_id, model_id, year, price, mileage, etc.

car_models:
model_id, make_id, model_name

car_makes:
make_id, make_name

Then you use a query like so:


SELECT model_name, make_name, year, price, mileage FROM cars c LEFT JOIN car_models cmo USING (model_id) LEFT JOIN car_makes cma USING (make_id) ORDER BY etc. etc.

Open in new window

0
 

Author Comment

by:baumli1
ID: 21787290
Although it doesn't answer the question, in the long run this will be easier and better for other selects.
0
 
LVL 3

Expert Comment

by:Xorlev
ID: 21787302
When I was writing the solution for the initial question I noticed how messy (and difficult) it'd make the query to have both make AND model in the same table. This will simplify things immediately.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21787571
I do agree with the above suggestion.

here is the answer to the original question:
$query1  = "SELECT cma.name car_model, cmo.name car_make, c.car_year, c.car_price, c.car_mileage, c.car_avail 
FROM site_cars c
JOIN site_car_makes cma
  ON cma.make_id = c.car_make
JOIN site_car_makes cmo
  ON cmo.make_id = c.car_model
ORDER BY $sortby";

Open in new window

0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
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 …

706 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

18 Experts available now in Live!

Get 1:1 Help Now