Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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 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 …

910 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

22 Experts available now in Live!

Get 1:1 Help Now