![](/img/trans.png)
[英]mysql join query for select rows from two tables that one table is having multiple rows matching to the first table
[英]MySQL How to limit amount of rows from JOIN-Query only from first Table and not the Second Table?
我有兩個表,一個是國家(地區),第二是城市。
第一表是國家
country_id | 國家的名字
第二表是城市
country_id | city_id | 城市名
我想限制從第二個表格到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] =>數組([country_name] =>國家1 [city_name] =>城市1)
[1] =>數組([country_name] =>國家1 [city_name] =>城市2)
[2] =>數組([國家名稱] =>國家1 [城市名稱] =>城市3)
我需要這樣的東西
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.