0

我目前有这个 SQL 语句:

SELECT p.*,k.name FROM posts AS p 
    LEFT JOIN `post-keywords` AS pk ON p.id = pk.id_post 
    LEFT JOIN keywords AS k ON pk.id_keyword = p.id
    ORDER BY p.id DESC LIMIT :num,:count

但是,它没有按预期工作。这是服务器返回的内容:

array(7) {
  [0]=>
  array(5) {
    ["id"]=>
    string(1) "2"
    ["title"]=>
    string(28) "Everything about Internet"
    ["text"]=>
    string(41) "Article itself"
    ["date"]=>
    string(1) "0"
    ["name"]=>
    NULL
  }
  [1]=>
  array(5) {
    ["id"]=>
    string(1) "1"
    ["title"]=>
    string(16) "Internet"
    ["text"]=>
    string(32) "Text text text"
    ["date"]=>
    string(10) "1371186330"
    ["name"]=>
    string(7) "tag1"
  }
  [2]=>
  array(5) {
    ["id"]=>
    string(1) "1"
    ["title"]=>
    string(16) "Internet"
    ["text"]=>
    string(32) "Text text text"
    ["date"]=>
    string(10) "1371186330"
    ["name"]=>
    string(7) "tag2"
  }
  [3]=>
  array(5) {
    ["id"]=>
    string(1) "1"
    ["title"]=>
    string(16) "Internet"
    ["text"]=>
    string(32) "Text text text"
    ["date"]=>
    string(10) "1371186330"
    ["name"]=>
    string(7) "tag3"
  }
  [4]=>
  array(5) {
    ["id"]=>
    string(1) "1"
    ["title"]=>
    string(16) "Internet"
    ["text"]=>
    string(32) "Text text text"
    ["date"]=>
    string(10) "1371186330"
    ["name"]=>
    string(7) "tag4"
  }
  [5]=>
  array(5) {
    ["id"]=>
    string(1) "1"
    ["title"]=>
    string(16) "Internet"
    ["text"]=>
    string(32) "Text text text"
    ["date"]=>
    string(10) "1371186330"
    ["name"]=>
    NULL
  }
  [6]=>
  array(5) {
    ["id"]=>
    string(1) "1"
    ["title"]=>
    string(16) "Internet"
    ["text"]=>
    string(32) "Text text text"
    ["date"]=>
    string(10) "1371186330"
    ["name"]=>
    NULL
  }
}

name如何从服务器中检索包含多个字段的数组:

array (5) {
   ["id"] =>
   string (1) "1"
   ["title"] =>
   string (16) "Internet"
   ["text"] =>
   string (32) "text text text"
   ["date"] =>
   string (10) "1371186330"
   ["name"] =>
   string (10) "word1"
   ["name"] =>
   string (10) "word2"
   ["name"] =>
   string (10) "word3"
}
4

1 回答 1

1

目前,您的查询是“覆盖”具有多个同名条目的字段。例如,您正在获取p.namek.name。您的查询没有任何区别,因此它被覆盖为 (I presume) k.name

为确保您同时获得它们,请编写如下内容:SELECT p.name AS p_name, k.name AS k_name

于 2013-08-26T09:04:31.167 回答