选择表格中的所有项目,并依次显示它们,具体取决于他们的父母标识 [英] Select all items in a table and display them in order depending on their parent Id's

查看:96
本文介绍了选择表格中的所有项目,并依次显示它们,具体取决于他们的父母标识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目表,一个类型表和一个部门表。我需要选择所有非常容易的项目,但我想按部门和类型对它们进行排序,然后按照它们类型和部门的标题的顺序显示它们。



我想先按部门列值对它们进行排序(网页上会有3列)。然后按每个列的Order值排序每个列中的部门。然后通过CreatedDate(与第一个添加项目的创建日期相同)对每个类型进行排序。然后通过该项目的CreatedDate对每个类型下的每个项目进行排序。

再次,我可以轻松地在部门中执行select *,然后对这些项目进行foreach的类型,然后foreach选择的项目,但它是混乱和缓慢,当我有成千上万的项目。



表:

 项目 - 标识,类型标识,标题,创建日期
类型 - 标识,部门标识,标题,创建日期
部门 - 标识,标题,列,顺序

示例数据:

 项目
1 - 1 - Apple - 2010年5月29日
2 - 2 - 玉米 - 2010年05月28日
3 - 1 - 橙色 - 05/27/2010
4 - 3 - 牛肉 - 2010年5月22日
5 - 4 - 螺丝刀 - 2010年5月22日
6 - 4 - Hammer - 2010年5月23日
7 - 1 - 香蕉 - 2010年5月24日
8 - 5 - 灯泡 - 2010年5月22日
9 - 6 - Fork - 09/26/2013
10 - 6 - 勺子 - 2013年9月27日

类型
1 - 1 - 水果 - 05 / 24/2010
2 - 1 - 蔬菜 - 05/28/2010
3 - 1 - M吃饭 - 2010年5月22日
4 - 2 - Handtools - 2010年5月22日
5 - 4 - 照明 - 2010年5月22日
6 - 3 - 用具 - 2013年9月26日

部门
1 - 食品 - 1 - 1
2 - 工具 - 2 - 1
3 - 厨房 - 3 - 2
4 - 客厅 - 3 - 1

这将在实际的列中按div或表格显示,如下所示:




第1列



食物




    • 牛肉


  1. 水果

    • 香蕉

    • 橙色

    • Apple


  2. 蔬菜

    • 玉米

    • >




      栏2



      工具 p>


      1. 手动工具

        • 螺丝刀b







      第3栏



      客厅


      1. 照明

        • 指示灯



      厨房
      $ b


      1. 餐具

        • 餐叉

        • 勺子






      解决方案

      我认为您需要首先正确加入所有这些表一起得到一个大型的数据集,然后使用PHP来检查何时显示一个不同的标题:

      $ $ p $ SELECT
      i.Title,
      t.Title,
      d.Title
      FROM
      项目i
      LEFT JOIN类型t
      ON i.type_id = t .Id
      LEFT JOIN部门d
      ON t.Department_Id = d.Id
      ORDER BY
      d.Column ASC,
      d.Order ASC,
      t.CreateDate ASC,
      i.CreateDate ASC


      I have an item table, a type table, and a department table. I need to select all items which is pretty easy, but I want to sort them by department and type and then display them in that order with the headers of their type and department.

      I would want to sort them by the Department "Column" value first (there will be 3 columns on the webpage). Then order the departments in each column by the "Order" value of each. Then sort each Type by "CreatedDate" (which is the same as the first added item's created date). Then sort each item under each type by the "CreatedDate" of that item.

      Again I can easily do a select * from departments, and then foreach of those do a select of types, and then foreach a select of items, but it is messy and slow when I have thousands of items.

      Tables:

      Items - Id, Type_Id, Title, CreatedDate
      Types - Id, Department_Id, Title, CreatedDate
      Departments - Id, Title, Column, Order
      

      Example Data:

      Items
      1 - 1 - "Apple" - 05/29/2010
      2 - 2 - "Corn" - 05/28/2010
      3 - 1 - "Orange" - 05/27/2010
      4 - 3 - "Beef" - 05/22/2010
      5 - 4 - "Screwdriver" - 05/22/2010
      6 - 4 - "Hammer" - 05/23/2010
      7 - 1 - "Banana" - 05/24/2010
      8 - 5 - "Lamp" - 05/22/2010
      9 - 6 - "Fork" - 09/26/2013
      10 - 6 - "Spoon" - 09/27/2013
      
      Types
      1 - 1 - "Fruit" - 05/24/2010
      2 - 1 - "Vegetable" - 05/28/2010
      3 - 1 - "Meat" - 05/22/2010
      4 - 2 - "Handtools" - 05/22/2010
      5 - 4 - "Lighting" - 05/22/2010
      6 - 3 - "Utensils" - 09/26/2013
      
      Departments
      1 - "Food" - 1 - 1
      2 - "Tools" - 2 - 1
      3 - "Kitchen" - 3 - 2
      4 - "Living Room" - 3 - 1
      

      This would display like the following in actual columns by divs or a table:


      Column 1

      Food

      1. Meat
        • Beef
      2. Fruit
        • Banana
        • Orange
        • Apple
      3. Vegetable
        • Corn


      Column 2

      Tools

      1. Handtools
        • Screwdriver
        • Hammer


      Column 3

      Living Room

      1. Lighting
        • Lamp

      Kitchen

      1. Utensils
        • Fork
        • Spoon


      解决方案

      I think you need to start off with properly joining all of the tables together to get one large conglomerate of data and then use PHP to check when to display a different header:

      SELECT
          i.Title,
          t.Title,
          d.Title
      FROM
          Items i
          LEFT JOIN Types t
              ON i.type_id = t.Id
          LEFT JOIN Departments d
              ON t.Department_Id = d.Id
      ORDER BY
          d.Column ASC,
          d.Order ASC,
          t.CreateDate ASC,
          i.CreateDate ASC
      

      这篇关于选择表格中的所有项目,并依次显示它们,具体取决于他们的父母标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆