Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mySQL、PHP、HTML表,具有可排序列-但存在一些列值问题_Php_Html_Mysql_Sorting_Html Table - Fatal编程技术网

mySQL、PHP、HTML表,具有可排序列-但存在一些列值问题

mySQL、PHP、HTML表,具有可排序列-但存在一些列值问题,php,html,mysql,sorting,html-table,Php,Html,Mysql,Sorting,Html Table,我有一个mySQL数据库表,其列如下: 项目 项目编号 名字 大小(例如,以字节为单位) 描述 日期 还有另一个表,与第一个表有1:*关系,其中包含描述第一个表中字段表示的项的“关键字”: Item_关键字 项目编号 关键词 我使用PHP从Item表中“select*”,然后迭代返回的行(使用fetch_assoc()的while循环)以构建包含所有数据的HTML表 在while循环的每次迭代中,我对Item_关键字表中与Item表中当前元组id匹配的所有关键字执行另一个查询,然后将这些

我有一个mySQL数据库表,其列如下:

项目
  • 项目编号
  • 名字
  • 大小(例如,以字节为单位)
  • 描述
  • 日期
还有另一个表,与第一个表有1:*关系,其中包含描述第一个表中字段表示的项的“关键字”:

Item_关键字
  • 项目编号
  • 关键词
我使用PHP从Item表中“select*”,然后迭代返回的行(使用fetch_assoc()的while循环)以构建包含所有数据的HTML表

在while循环的每次迭代中,我对Item_关键字表中与Item表中当前元组id匹配的所有关键字执行另一个查询,然后将这些关键字添加到表中的一列,并连接在一起:

$res = getItems();
if ($res)
{
  while ($row = $res->fetch_assoc())
  {
    ...
    $itemID = $row['item_id];
    $res2 = getKeywords($itemID);
    if ($res2)
    {
      $keywords = "";
      while ($row2 = $res2->fetch_assoc())
      {
        if (strlen($keywords) > 0)
          $keywords .= ", ";
        $keywords .= $row2['keyword'];
      }
      $text .= "<td> " . $keywords . " </td>";
     }
   }
}
我正在尝试使HTML表列可排序。我可以想象,在我对Item表的查询中,orderby是如何工作的。问题是,由于关键字存储在一个单独的表中,并通过一个独立的查询检索,因此我只能按一组查询结果或另一组查询结果排序,但不能同时按这两组结果排序——更简而言之,我看不到任何按关键字列中的值排序的方法

是否有某种我可以构造的深奥查询,允许我为项目选择关键字,并将这些关键字包含在项目表上查询的“FROM”参数中?比如:

ID  Name    Size  Description  Date    Keywords
0   Widget  1024  Widget one   010101  widget,one,item
SELECT item_id, name, size, description, date (SELECT keyword FROM Item_Keyword WHERE item_id = 0)
FROM Item
WHERE item_id = 0

首先,您可能需要通过以下单个查询获取所有数据:

SELECT
  i.item_id AS `item_id`,
  i.name AS `name`,
  i.size AS `size`,
  i.description AS `description`,
  i.date AS `date`,
  GROUP_CONCAT(ik.keyword) AS `keywords`
FROM item AS i
LEFT JOIN item_keyword AS ik
  ON item.item_id = item_keyword.item_id
GROUP BY i.item_id
ORDER BY i.item_id ASC, ik.keyword ASC /* OR whatever sort you desire */
请注意,我在这里使用了
GROUP\u CONCAT()
GROUP BY
子句,以便将所有关键字条目折叠到与每个项目id关联的一行中


这将在一个数据库调用中获得所需的一切,从而消除所有不必要的额外查询。您也可以在没有分组依据的情况下执行此操作,您只需要有更多行(即,一个包含3个关键字的项目将产生3行)。使用这种方法,您可以在呈现表之前将数据读入多维数组。

在选择查询中使用自然连接并一次提取所有数据这可能是一种不太理想的方法,具体取决于您的程序,但您可以在生成表后使用jQuery对其进行排序。太棒了,非常感谢你分享你的知识。我从来没有在查询写作方面超越过“绝对初学者”,你的例子证明了更高级学习的力量。非常感谢。@AMarch没问题。请注意,如果将来您看到自己在这样的循环中查询数据库,这可能表明存在一种反模式,最好通过修改检索数据的方法来处理。