你能告诉我做嵌套选择语句的更好的选择吗?
正在处理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........
       }
      }
     }
    }
   }
  }
 }
}

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

最佳答案

这似乎是一个INNER JOIN是可能的:

SELECT sl0.*, sl1.* FROM stackover_link sl0
    INNER JOIN stackover_link sl1 ON sl0.link_to = sl1.link_from and sl1.link_level = 1

...

WHERE sl0.link_from='123456' AND sl0.link_level='0';

关于php - SQL优化,内在? (RBAC),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35653424/

10-11 05:23