K
K
Konstantin2016-04-07 11:37:35
MySQL
Konstantin, 2016-04-07 11:37:35

How to correctly compose an SQL query, 3 tables, the same field names?

SQL worker,
4e8e01f6b25f4cf189c5e1253c4205cd.png
I see that I chose all 3 fields as I need
But does not want to work in the script, it gives out only Russia

$sql = "SELECT city.name, region.name, country.name
FROM city, region, country
WHERE city.city_id = ?i AND region.region_id = ?i AND  country.country_id = ?i";

$geo = $db->getall($sql,$ank['city_id'],$ank['regionid'],$ank['countryid']);

array (size=1)
  0 => 
    array (size=1)
      'name' => string 'Россия' (length=12)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
George, 2016-04-07
@Esionru

$sql = "SELECT city.name as city_name, region.name as region_name , country.name as country_name
FROM city, region, country
WHERE city.city_id = ?i AND region.region_id = ?i AND  country.country_id = ?i";

$geo = $db->getall($sql,$ank['city_id'],$ank['regionid'],$ank['countryid']);

D
Dmitry Kovalsky, 2016-04-07
@dmitryKovalskiy

For a script with CROSS JOIN separated by commas, it would be nice to shoot you. How are table data related?
Ideally the script should look something like this:

select something from city as c
INNER JOIN region as r ON c.regionId = r.Id
INNER JOIN country as co ON co.Id = r.countryId
WHERE some condition

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question