Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

876 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