Jump to content

retrieving mysql data with table joins using one query


unistake

Recommended Posts

Hi all,

 

I am trying to get information from mysql by firstly joining a couple of tables such as:

 

"SELECT Latitude, Longitude  FROM airports INNER JOIN rosters ON airports.IATA = rosters.Dep WHERE rosters.Dep = 'BGY'";

 

I would also however like to select another airports.Latitude and airports.Longitude in the same query.

Something like...

 

"SELECT Latitude AS departure_lat, Longitude AS depatrure_lng FROM airports INNER JOIN rosters ON airports.IATA = rosters.Dep WHERE rosters.Dep = 'BGY'

AND

SELECT airports.Latitude AS arrival_lat, airports.Longitude AS arrival_lng FROM airports INNER JOIN rosters ON airports.IATA = rosters.Arr WHERE rosters.Arr = 'CRL'";

 

So basically, I am trying to get the departure and destination latitude and longitudes from an airport where departure = BGY and arrival = CRL using one query.

 

Thanks for any help!

Link to comment
Share on other sites

Thanks Barand,

 

although I dont quite have the grasp of it. I knew I should be using the 'AS'.

 

This is what I have so far:

$sql = "SELECT * FROM rosters WHERE Code = '$Code' AND SectorDate >= '$today' AND Arr != '' GROUP BY RostersID ASC";
$result = mysqli_query($cxn,$sql) or die ("Cant do it!");
while($row=mysqli_fetch_assoc($result)) {
	$sql = "SELECT 
	  d.Latitude AS departure_lat
	, d.Longitude AS depatrure_lng 
	, a.Latitude AS arrival_lat
	, a.Longitude AS arrival_lng 
	FROM rosters 
	INNER JOIN airport a ON a.IATA = rosters.Arr 
	INNER JOIN airport d ON d.IATA = rosters.Dep 
	WHERE rosters.Arr = '{$row['Arr']}'
		AND rosters.Dep = '{$row['Dep']}'";
	echo $sql;
	$result = mysqli_query($cxn,$sql)
		or die ("no can do");
	while($row=mysqli_fetch_assoc($result)) {
		echo $row['departure_lat'];
	}
}

`rosters` table =

RostersID      Dep      Arr       SectorDate

1                     BGY      CRL      2016-01-22

2                     CRL      BGY      2016-01-22

 

`airports` table =

AirportsID     IATA      Latitude          Longitude     

1                     BGY     48.5320           -3.2400

2                     CRL     50.3400           -1.4200

Link to comment
Share on other sites

Don't run queries inside loops - it eats server resources. Combine the two queries into one.

SELECT 
  d.Latitude AS departure_lat
, d.Longitude AS depatrure_lng 
, a.Latitude AS arrival_lat
, a.Longitude AS arrival_lng 
FROM rosters 
INNER JOIN airport a ON a.IATA = rosters.Arr 
INNER JOIN airport d ON d.IATA = rosters.Dep 
WHERE rosters.Arr = 'CRL'
	AND rosters.Dep = 'BGY';
	AND Code = '$Code' 
	AND SectorDate >= '$today' 
ORDER BY RostersID 
Link to comment
Share on other sites

hmm, still not working here.

 

I changed a few spelling mistakes above and input 'rosters.' inside a couple of the WHERE clauses.

 

Still get the error 'no can do'

$sql = "SELECT 
  d.Latitude AS departure_lat
, d.Longitude AS departure_lng 
, a.Latitude AS arrival_lat
, a.Longitude AS arrival_lng 
FROM rosters 
INNER JOIN airports a ON a.IATA = rosters.Arr 
INNER JOIN airports d ON d.IATA = rosters.Dep 
WHERE rosters.Arr = 'CRL'
	AND rosters.Dep = 'BGY'
	AND rosters.Code = '$Code' 
	AND rosters.SectorDate >= '$today' 
ORDER BY rosters.RostersID";
	//echo $sql;
	$result = mysqli_query($cxn,$sql)
		or die ("no can do");
	while($row=mysqli_fetch_assoc($result)) {
		echo $row['departure_lat'];
	}
Edited by unistake
Link to comment
Share on other sites

I have the error,

 

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /customers/5/5/7/website.com/httpd.www/page.php on line 56 Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /customers/5/5/7/website.com/httpd.www/page.php on line 40

$sql = "SELECT * FROM rosters WHERE Code = '$Code' AND SectorDate >= '$today' AND Arr != '' GROUP BY RostersID ASC";
$result = mysqli_query($cxn,$sql) or die ("Cant do it!");
while($row=mysqli_fetch_assoc($result)) {  // LINE 40
	$sql = "SELECT 
	  d.Latitude AS departure_lat
	, d.Longitude AS departure_lng 
	, a.Latitude AS arrival_lat
	, a.Longitude AS arrival_lng 
	FROM rosters 
	INNER JOIN airports a ON a.IATA = rosters.Arr 
	INNER JOIN airports d ON d.IATA = rosters.Dep 
	WHERE rosters.Arr = 'ALC'
		AND rosters.Dep = 'MAN'
		AND rosters.Code = '$Code' 
		AND rosters.SectorDate >= '$today' 
	ORDER BY rosters.RostersID";
	$result = mysqli_query($cxn,$sql)
		or $cxn->error;
	while($row=mysqli_fetch_assoc($result)) {  //LINE 56
		echo $row['departure_lat'];
	}
}
Link to comment
Share on other sites

Change

die ("Cant do it!");

to

die ($cnx->error);

 

Then we can see why the first query is failing.

 

You should not be running that first query any more. I said you should NOT run queries inside loops and you are still doing it. You should just be running the one combined query and process it's results.

 

 

EDIT : How many rows in your tables?

Edited by Barand
  • Like 1
Link to comment
Share on other sites

Those files are not particularly large, so I am baffled by that MAX_JOIN_SIZE error. The default is 18,446,744,073,709,551,615 which is a few more more than 1,352,000,000 (the cartesian product of your query, which is the number of rows returned if every record in the tables was joined with every record in the other tables ie 20000 x 260 x 260).

 

Did you get an error message for that first query?

Link to comment
Share on other sites

You use $result for the result of the first query and then use that same variable again inside the loop. Another reason not to be calling queries inside loops.

 

As I said, get rid of that first query and replace it with the second, and just process those results.

 

Are you running on a live host?

Link to comment
Share on other sites

And run this query (in phpMyAdmin or similar) and see what the result is

SHOW VARIABLES LIKE 'max_join%';

EG

mysql> SHOW VARIABLES LIKE 'max_join%';
+---------------+----------------------+
| Variable_name | Value                |
+---------------+----------------------+
| max_join_size | 18446744073709551615 |
+---------------+----------------------+
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.