Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-06-14
4
Medium Priority
?
1,190 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
[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 3

Accepted Solution

by:
Xorlev earned 500 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This article discusses how to implement server side field validation and display customized error messages to the client.
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.

715 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