473,396 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

SQL JOIN in PHP - is it possible to retrieve two different values from one column?

Hey guys,

my first time in this forum, i have tried looking for the solution to my problem on this forum as well as others, however i have not found an answer yet. would much appreciate your help. I am building a web based database using php and mysql. i think this problem is more associated with mysql.

basically i have a two tables in the database: Location, Distance.

Location consists of the fields: Location_ID, name.
Distance consists of the fields: Distance_ID, origin, destination, distance.

the two fields origin and destination in the distance field, reference the Location_id.

what i am wondering is, is it possible to create a query that will out put a table with the following details: Distance_ID, Origin_name, Destination_name, distance.

for example:
Location_ID, name
1, london
2, paris
3, rome

Distance_ID, origin, destination, distance
1, 1, 2, 50
2, 1, 3, 75
3, 3, 2, 12

what i want to display is:
Distance_ID, Origin, Destination, Distance
1, london, paris, 50
2, london, rome, 75
3, rome, paris, 15

the code that i have so far is:

Expand|Select|Wrap|Line Numbers
  1. $query="SELECT DistanceID, Location.name, Location.name, distance FROM Distance INNER JOIN Location ON Distance.origin=Location.locationID";
  2. $result=mysql_query($query);
  3. $num=mysql_numrows($result);
  4. mysql_close();
  5. $i=0;
  6.  
  7. if ($num==0){
  8. echo "<b>Sorry, there are no distances.</b>";
  9. }
  10.  
  11. else while ($i < $num) {
  12.  
  13. $DistanceID=mysql_result($result,$i,"DistanceID");
  14. $origin=mysql_result($result,$i,"Location.name");
  15. $destination=mysql_result($result,$i,"Location.name");
  16. $distance=mysql_result($result,$i,"distance");
  17.  
  18. echo "<tr><td>$DistanceID</td><td>$origin</td><td>$destination</td><td>$distance</td></tr>";
  19. $i++;
  20. }
what my code will output is :

Distance_ID, Origin, Destination, Distance
1, london, london, 50
2, london, london, 75
3, rome, rome, 15


this is not exactly what i want. i can sort of see where the problem is (the issue with Location.name), but i have no idea how to solve this, or whether it is even possible. i am not familiar with INNER JOIN, but i understand that
Expand|Select|Wrap|Line Numbers
  1. INNER JOIN Location ON Distance.origin=Location.locationID
gets stored in origin
Expand|Select|Wrap|Line Numbers
  1. $origin=mysql_result($result,$i,"Location.name")
aswell as destination
Expand|Select|Wrap|Line Numbers
  1. $destination=mysql_result($result,$i,"Location.name");
. and that only
Expand|Select|Wrap|Line Numbers
  1. INNER JOIN Location ON Distance.origin=Location.locationID
is declared for origin. but if i also do it for destination, the code breaks. i.e.
Expand|Select|Wrap|Line Numbers
  1. "SELECT DistanceID, Location.name, Location.name, distance FROM Distance INNER JOIN Location ON Distance.origin=Location.locationID INNER JOIN Location ON Distance.destination=Location.locationID"

any ideas on how to display the names of both the origin and destination would be much appreciated.


edit: i guess i could always create 2 tables named location_origin and location_destination, but my idea was to save space by just having one table to serve the purpose of storing names of places. though i wonder if it would be a better/easier idea to have the two tables instead of one.
Jan 30 '10 #1
2 2034
Expand|Select|Wrap|Line Numbers
  1. select distance_id, a1.name , a2.name from location a1 a2, distance where a1.name=$origin and a2.name=$destination;
I know this query is buggy but i think this should enlighten you that Inner join is not so neccessary.

PS: i am sql newbie ;)
Feb 1 '10 #2
shabinesh,

thanks for your reply, i haven't tried your code, but i got some help from a different forum earlier which worked for me.

<Snipped. Please read the guidelines before posting -- Atli>

thanks for your reply though!
Feb 1 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Dave | last post by:
I have 2 tables, one with names, and another with addresses, joined by their CIVICID number (unique to the ADDRESSINFO table) in Oracle. I need to update a field in the NAMEINFO table for a...
0
by: Preston Landers | last post by:
Hello all. I am trying to write a query that "just" switches some data around so it is shown in a slightly different format. I am already able to do what I want in Oracle 8i, but I am having...
6
by: Ray | last post by:
Group, Passing inline values to a udf is straightforward. However, how or is it possible to pass a column from the select of one table into a udf that returns a table variable in a join to the...
7
by: stabbert | last post by:
I am attempting to join two tables together on two different unix servers. Here is some relevant info about the tables. TABLE 1 Setup ----------------------- DB2 UDB 7.2 EE
4
by: PASQUALE | last post by:
Hi I have a question: do the both statements below give the same result? If yes then does somebody know something about preformance differencies using these joins? SELECT A.* FROM Table1 A...
4
by: Anthony Robinson | last post by:
I was actually just wondering if someone could possibly take a look and tell me what I may be doing wrong in this query? I keep getting ambiguous column errors and have no idea why...? Thanks in...
2
by: Notgiven | last post by:
I have three tables: table1: table2_ID table3_ID complete table3: table3_ID name
7
by: Shanimal | last post by:
I would like to know how to join 2 queries so that the results of these 2 queries show up in the same query: SELECT b.bios_serial_number FROM bios b: SELECT s.system_name FROM system s; ...
6
by: Mike S | last post by:
Hi all, A (possibly dumb) question, but I've had no luck finding a definitive answer to it. Suppose I have two tables, Employees and Employers, which both have a column named "Id": Employees...
4
by: Yogesh Sharma | last post by:
create table stuinfo(roll int,name varchar(10)) insert into stuinfo values(1,'sonia') insert into stuinfo values(2,'usha') select * from stuinfo create table marks(roll int,grade varchar)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.