du656637962 2013-10-11 08:47
浏览 42
已采纳

选择sql多对多关系到嵌套的php对象中

problem

I have two data tables SEQUENCES and ORGANISMS whose many-to-many-relationship is mappend in the table SOURCES. There is also a 1-m relationshipt between SOURCES and ENTRIES. I will append a detailed structure.

What i want to achieve, is the display of all sequences with all associated organisms and entries, where a condition within the sequences table is met. I have some ideas on how to achieve this, but i need the solution with the best performance, as each of these contains 50k+ entries.

idea one

Select all organisms that belong to the same sequence as a concatenated string in sql, and split it in PHP. I have no idea though, how to do the concatenation in SQL.

idea two

select same sequences with different organisms as distinct records, order by organism, and join them later in php. though this somehow feels just wrong.

idea three

use views. ANY idea on this one appreciated

structure

SEQUENCES
  SEQUENCE_ID
  DESCRIPTION

ORGANISMS
  ORGANISM_ID
  NAME

SOURCES
  SOURCE_ID
  SEQUENCE_ID FK to SEQUENCES.SEQUENCE_ID
  ORGANISM_ID FK to ORGANISMS.ORGANISM_ID

ENTRIES
  SOURCE_ID FK to SOURCES.SOURCE_ID
  ENTRY_VALUE

desired outcome

array(
  array(
    "SEQUENCE_ID" => 4,
    "DESCRIPTION" => "Some sequence",
    "SOURCES" => array(
      array(
        "ORGANISM_ID" => 562,
        "ORGANISM_NAME" => "Escherichia coli",
        "ENTRIES" => array(
          "some entry",
          "some other entry"
        ),
      array(
        "ORGANISM_ID" => 402764,
        "ORGANISM_NAME" => "Aranicola sp. EP18",
        "ENTRIES" => array()
      )
    )
  ),
  array(
    "SEQUENCE_ID" => 5,
    .....
  )
)

PHP5 and FIREBIRD2.5.1

  • 写回答

1条回答 默认 最新

  • duanbushi1479 2013-10-11 09:05
    关注

    You can't fetch a nested array like that directly from a flat table structure. But if I get you right, what you want to do is not that hard to achieve.

    I don't understand why you would concatenate things and then split them again, that's hard to maintain and probably slow.

    I see two approaches here:

    1. Fetch everything at once as flat table using JOIN and loop through it in PHP. This approach creates a lot of duplication but it's fast because you can fetch all data in one query and then process it with PHP.

    2. Fetch every entity separately, loop and fetch the next hierarchy level as you go. This approach will be slower. It takes complexity away from the SQL query and doesn't fetch redunant data. It also gives you more freedom as to how you loop through your data and what you do with it.

    Alternatively you might want to actually store hierarchical data in a no-sql way, where you could already store the array structure you mentioned.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题