SQL优化,内在? (RBAC)

时间:2016-02-26 13:48:42

标签: php mysql select optimization rbac

你能告诉我做嵌套select语句的更好选择吗? 我正在研究rbac项目,我需要获得表的特权。 现在这段代码完美无缺,但如果表中有很多数据,查询计数会变大。

$DB_Query_AID = $DB_Cnx->query("SELECT * FROM stackover_link WHERE link_from='123456' AND link_level='0';");
while($DB_DataAID = $DB_Query_AID->fetch()) {
 if(!empty($DB_DataAID['LID'])) {
  $AID = $DB_DataAID['link_to'];
  $DB_Query_BID = $DB_Cnx->query("SELECT * FROM stackover_link WHERE link_from='$AID' AND link_level='1';");
  while($DB_DataBID = $DB_Query_BID->fetch()) {
   if(!empty($DB_DataBID['LID'])) {
    $BID = $DB_DataBID['link_to'];
    $DB_Query_CID = $DB_Cnx->query("SELECT * FROM stackover_link WHERE link_from='$BID' AND link_level='2';");
    while($DB_DataCID = $DB_Query_CID->fetch()) {
     if(!empty($DB_DataCID['LID'])) {
      $CID = $DB_DataCID['link_to'];
      $DB_Query_DID = $DB_Cnx->query("SELECT * FROM stackover_link WHERE link_from='$CID' AND link_level='3';");
      while($DB_DataDID = $DB_Query_DID->fetch()) {
       if(!empty($DB_DataDID['LID'])) {
        //foooooooooooooooo........
       }
      }
     }
    }
   }
  }
 }
}

只有1个查询可以做同样的事吗? 谢谢你的帮助。

1 个答案:

答案 0 :(得分:2)

这似乎是<h3>None of the elements selected - Not only one of its type</h3> <div> <p class="eventPerformanceDate">Paragraph 1</p> <p class="eventPerformance">Paragraph 2</p> </div> <h3>Element is selected - Only p and has required class</h3> <div> <p class="eventPerformance">Paragraph 1</p> </div> <h3>Elements are selected - Both are only element of their type and have class</h3> <div> <h4 class="eventPerformance">Heading 1</h4> <p class="eventPerformance">Paragraph 1</p> </div> <h3>Element is not selected - Only p but doesn't have required class</h3> <div> <p class="some-other-class">Paragraph 1</p> </div>

INNER JOIN