我有两个表,一个是国家,第二个是城市。
第一个表格是国家
country_id|country_name
第二张表是城市
国家|城市|城市|名称
我想将第二个表中的查询数量限制为3个城市。因为一个国家有很多城市。我只需要5个城市。
我的查询:
$query = “SELECT c.country_name, p.city_name FROM (SELECT * FROM Cities LIMIT 3) AS p LEFT JOIN Countries as c ON p.county_id = c.county_id”;ter code here
$result = mysqli_query($db, $query);
$my_array = array();
while($row = mysqli_fetch_assoc){
array_push($my_array, $row);
}
如果我填写$my_array我从数据库得到3个结果
我的输出:
[0] =
[1] =
[2] =
我需要这样的东西
County 1 ----------------------- City 1 of Country 1 City 2 of Country 1 City 3 of Country 1 City 4 of Country 1 City 5 of Country 1 County 2 ----------------------- City 1 of Country 2 City 2 of Country 2 City 3 of Country 2 City 4 of Country 2 City 5 of Country 2 County 3 ----------------------- City 1 of Country 3 City 2 of Country 3 City 3 of Country 3 City 4 of Country 3 City 5 of Country 3
我做错了什么?我使用MySQL 5.6。26
谢谢你。
像那样吗?
SELECT city_id, city_name FROM Cities AS p LEFT JOIN Countries AS c ON c.country_id = p.country_id WHERE c.country_id = 'id' LIMIT 0,5