centenial Posted June 30, 2006 Share Posted June 30, 2006 Hello,I have three MySQL tables:1. table "listings" (fields below)a. listing_numberb. agent_idc. category_id2. table "agents" (fields below)a. idb. name3. table "categories" (fields below)a. idb. nameI want to display the information in one table, with these columns:1. Listing Number2. Agent3. CategoryI want to display the "Agent" and "category" names instead of the agent and category ids. Can this be done with a MySQL Join Query?Thanks for your help, Quote Link to comment https://forums.phpfreaks.com/topic/13345-phpmysql-join-question/ Share on other sites More sharing options...
karthikeyan_coder Posted June 30, 2006 Share Posted June 30, 2006 do you need query alone or full coding for output? Quote Link to comment https://forums.phpfreaks.com/topic/13345-phpmysql-join-question/#findComment-51468 Share on other sites More sharing options...
centenial Posted July 1, 2006 Author Share Posted July 1, 2006 Just query, thanks :) Quote Link to comment https://forums.phpfreaks.com/topic/13345-phpmysql-join-question/#findComment-51470 Share on other sites More sharing options...
ssmK Posted July 1, 2006 Share Posted July 1, 2006 SELECTl.listing_numbera.agent,c.nameFROMlistings AS l,agents AS a,categories as cWHEREl.agent_id = a.idANDl.category_id = c.id; Quote Link to comment https://forums.phpfreaks.com/topic/13345-phpmysql-join-question/#findComment-51476 Share on other sites More sharing options...
centenial Posted July 1, 2006 Author Share Posted July 1, 2006 Ok, I think I'm going to need the full PHP code on that one... Could you please show a full example? Quote Link to comment https://forums.phpfreaks.com/topic/13345-phpmysql-join-question/#findComment-51481 Share on other sites More sharing options...
ssmK Posted July 1, 2006 Share Posted July 1, 2006 [code]<?php// UNTESTED CODEDEFINE ('DB_USER', 'username');DEFINE ('DB_PASSWORD', 'password');DEFINE ('DB_HOST', 'localhost');DEFINE ('DB_NAME', 'sitename');$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() );@mysql_select_db (DB_NAME) OR die ('Could not select the database: ' . mysql_error() );$query = "SELECT l.listing_number, a.agent, c.name FROM listings AS l, agents AS a, categories as c WHERE l.agent_id = a.id AND l.category_id = c.id;";$result = @mysql_query($result);while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) { //DO SOMETHING WITH UR ROWS HERE //$row['agent'] for example}if($result) { //THE QUERY WAS SUCCESSFUL} else { //IT WAS NOT SO SUCCESSFUL}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13345-phpmysql-join-question/#findComment-51494 Share on other sites More sharing options...
centenial Posted July 1, 2006 Author Share Posted July 1, 2006 [code]include 'includes/config.php';$sql = "select l.id, l.price, l.mls, a.name As agent, c.name As category, s.name As status from listings lINNER JOIN agents a ON a.id = l.a_idINNER JOIN categories c ON c.id = l.c_idINNER JOIN status s ON s.id = l.status";$res = mysql_query($sql,$conn) or die(mysql_error());while ($row = mysql_fetch_assoc($res)) { echo " <tr><td>"; print $row['mls'].'</td><td>'. $row['category'].'</td><td>'. $row['price'].'</td><td>'. $row['agent'].'</td><td>'. $row['status'].'</td><td><a href="listing.php?method=edit&id='. $row['id'].'">Edit</a> <a href="listing.php?method=delete&id='. $row['id'].'">Delete</a></td></tr>'; } echo '</table>';[/code]Why is this not working? Can someone please help? Quote Link to comment https://forums.phpfreaks.com/topic/13345-phpmysql-join-question/#findComment-51726 Share on other sites More sharing options...
.josh Posted July 1, 2006 Share Posted July 1, 2006 would you like to explain what is not working? do you get an error message? empty results? have you put that query directly into the database? did you get the desired results, therefore making it a php code related issue? Be more specific. Quote Link to comment https://forums.phpfreaks.com/topic/13345-phpmysql-join-question/#findComment-51731 Share on other sites More sharing options...
centenial Posted July 1, 2006 Author Share Posted July 1, 2006 Sorry, I should have been more specific - It's working now. I do have one other question - I want to be able to sort by users clicking on a column.Could someone tell me why this isn't working? I have the correct $GET variables - Is it a problem with the SQL query?[code]select l.id, l.mls, a.name As agent, c.name As category, t.name As town, s.name As status from listings l INNER JOIN agents a ON a.id = l.a_idINNER JOIN categories c ON c.id = l.c_idINNER JOIN towns t ON t.id = l.t_idINNER JOIN status s ON s.id = l.statusORDER BY $orderby // Which I get via the URL[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13345-phpmysql-join-question/#findComment-51750 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.