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
Solved

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

Posted on 2008-06-14
4
1,170 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 143

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 …

860 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