我很难理解如何查询1个表(位置)和加入(左,右或内)不确定。
表:位置 字段:locationID,locationParentID,locationName
locationID | locationParentID | LOCATIONNAME
1 | 0 |北爱尔兰 2 | 0 |英国 3 | 0 |苏格兰 4 | 1 |唐郡 5 | 1 |郡阿马 6 | 2 |伦敦 7 | 2 |利兹 8 | 3 |格拉斯哥 9 | 3 |爱丁堡 10 | 4 |贝尔法斯特 11 | 4 |纽敦纳兹
任何人都可以指出我正确的方向,以便我可以在1张桌子上为这个3级位置结构的某种连接执行1次查询吗?
非常感谢任何帮助。
SELECT * FROM locations(JOIN EACH SUB LOCATION AS ['sub']匹配LOCATION_PARENT_ID)ORDER BY locationParentID,locationName ASC。
SELECT * FROM _locations
JOIN _locations as parent_location
ON _locations.locationParentID = parent_location.locationID
JOIN _locations AS super_parent_location
ON parent_location.locationParentID = super_parent_location.locationID
Array
(
[0] => 117
[locationID] => 1
[1] => 19
[locationParentID] => 0
[2] => Hull
[locationName] => England
[3] => hull
[locationUri] =>
[4] => 2
[locationLevel] => 1
[5] => 19
[6] => 1
[7] => East Yorkshire
[8] =>
[9] => 2
[10] => 1
[11] => 0
[12] => England
[13] =>
[14] => 1
)
我想试试
Array
(
[locationID] => 1
[locationParentID] => 0
[locationName] => England
[locationUri] =>
[locationLevel] => 1
[sub'] => Array (
[locationID] => 55
[locationParentID] => 1
[locationName] => Leeds
[locationUri] =>
[locationLevel] => 2
[sub'] => Array (
[locationID] => 144
[locationParentID] => 55
[locationName] => Chorley
[locationUri] =>
[locationLevel] => 3))
)
现在真的很想让这个工作,如果有人能提供任何进一步的消息,我将非常感激。
我当前的查询是
SELECT * FROM _locations
JOIN _locations as parent_location
ON _locations.locationParentID = parent_location.locationID
JOIN _locations AS super_parent_location
ON parent_location.locationParentID = super_parent_location.locationID
输出所有字段,但我想使用mysqli_fetch_array($ q,MYSQL_ASSOC)返回3级深度数组;
再次感谢
答案 0 :(得分:0)
尝试这样的事情
SELECT * FROM location
JOIN location as parent_location
ON location.locationParentID = parent_location.locationID
JOIN location AS super_parent_location
ON parent_location.locationParentID = super_parent_location.locationID
基本上你只是一遍又一遍地加入同一个表并使用别名。
答案 1 :(得分:0)
SELECT * FROM _locations
JOIN _locations as parent_location
ON _locations.locationParentID = parent_location.locationID
JOIN _locations AS super_parent_location
ON parent_location.locationParentID = super_parent_location.locationID
Array
(
[0] => 117
[locationID] => 1
[1] => 19
[locationParentID] => 0
[2] => Hull
[locationName] => England
[3] => hull
[locationUri] =>
[4] => 2
[locationLevel] => 1
[5] => 19
[6] => 1
[7] => East Yorkshire
[8] =>
[9] => 2
[10] => 1
[11] => 0
[12] => England
[13] =>
[14] => 1
)
我想试试
Array
(
[locationID] => 1
[locationParentID] => 0
[locationName] => England
[locationUri] =>
[locationLevel] => 1
[sub'] => Array (
[locationID] => 55
[locationParentID] => 1
[locationName] => Leeds
[locationUri] =>
[locationLevel] => 2
[sub'] => Array (
[locationID] => 144
[locationParentID] => 55
[locationName] => Chorley
[locationUri] =>
[locationLevel] => 3))
)