Need guidance on multiple table query

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
jimdavidson
Forum Newbie
Posts: 13
Joined: Thu Jul 26, 2007 10:10 am

Need guidance on multiple table query

Post by jimdavidson »

I have a query that is looking at four tables
Mx_data
Bikes
Driver
Status

Two of the tables (bikes and Status) have fields with identical name (description). How can I get them both into a recordset? I'm assuming there is someway to set an alias or something like that.

Here's the query

$query_listMx = "SELECT Mx_data.bike_id, Mx_data.Mx_id, Mx_data.rpm, Mx_data.updated, bikes.`description`, driver.`name`, status.`description` FROM Mx_data, bikes, driver, status WHERE bikes.`bike_id`= Mx_data.bike_id AND Mx_data.`driver_id`= driver.driver_id AND Mx_data.`Mx_status` = status.`status_id` AND Mx_data.customer_id = $colname_listMx";
$query_limit_listMx = sprintf("%s LIMIT %d, %d", $query_listMx, $startRow_listMx, $maxRows_listMx);
$listMx = mysql_query($query_limit_listMx, $motorx) or die(mysql_error());
$row_listMx = mysql_fetch_assoc($listMx);

ANy help will be appreciated
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: Need guidance on multiple table query

Post by EverLearning »

You use aliases in Mysql like this
[sql]field_name AS field_alias[/sql]
So your query would be something like:

Code: Select all

 
SELECT bikes.`description` AS bike_desc, STATUS.`description` AS status_desc, 
/* rest of your query */
 
jimdavidson
Forum Newbie
Posts: 13
Joined: Thu Jul 26, 2007 10:10 am

Re: Need guidance on multiple table query

Post by jimdavidson »

Thank you very much!
Post Reply