SQL:循环遍历同一个表

时间:2014-04-16 14:59:14

标签: mysql sql

我有一张桌子" projectdetails"其中parent_id是同一表中列project_id的外键..

从下面的记录我只想要那些parent_id行,没有' recycle_bin`值为1的行,并且还应该显示creater_id = 7923的记录;

我有这样的记录:

mysql> SELECT *FROM  projectdetails;
+------------+-----------------+-----------+-------------+------------+
| project_id | project_name    | parent_id | recycle_bin | creater_id |
+------------+-----------------+-----------+-------------+------------+
|          0 | -               |         0 |           0 |       7898 |
|        100 | Country         |         0 |           2 |       7923 |
|        101 | animal          |         0 |           1 |       7923 |
|        102 | India           |       100 |           2 |       7923 |
|        103 | pakistan        |       100 |           2 |       7923 |
|        104 | cow             |       101 |           1 |       7923 |
|        105 | elephant        |       101 |           1 |       7923 |
|        109 | black elephent  |       105 |           1 |       7923 |
|        110 | white elephent  |       105 |           2 |       7923 |
|        111 | wild black elep |       109 |           1 |       7923 |
|        112 | simple blak elp |       109 |           1 |       7923 |
|        113 | lion            |       105 |           1 |       7923 |
|        114 | red lion        |       113 |           1 |       7923 |
|        115 | black lion      |       113 |           1 |       7923 |
|        116 | girls           |         0 |           1 |       7923 |
|        117 | good girls      |       116 |           1 |       7923 |
|        118 | funky girls     |       116 |           1 |       7923 | 
+------------+-----------------+-----------+-------------+------------+
7 rows in set (0.00 sec)

预期产出:

+------------+----------------+-----------+-------------+------------+
| project_id | project_name   | parent_id | recycle_bin | creater_id |
+------------+----------------+-----------+-------------+------------+ 
|        100 | Country        |         0 |           2 |       7923 | 
|        110 | white elephent |       105 |           2 |       7923 |
+------------+----------------+-----------+-------------+------------+

注意:这是树结构表,这里每个子id上的用户都可以插入很多其他记录。就像树结构一样。所以请回答与此说明相关的内容。

如果你无法得到我的要求,那么请写评论,我会试着解释你......

更新

所以基本上,当查询读取一行来决定它是否适用于显示时,

第一个查询流程:对国家说。

首先找一行说INDIA,&然后接下来看看它的parent_id,如果它有parent_id,那么转到那个parent_id(现在这是一个project_id = 100),所以接下来再看看它是否有父项,如果没有,则查看列recycle_bin ..如果1然后显示这个结果或否则忽略。

3 个答案:

答案 0 :(得分:1)

尝试此查询,

SELECT 
project_id,project_name,parent_id,recycle_bin,creater_id 
FROM projectdetails
WHERE recycle_bin not in (select recycle_bin from projectdetails where recycle_bin = 1 )
AND creater_id = 7923;

否则请尝试@SamD建议,

SELECT 
project_id,project_name,parent_id,recycle_bin,creater_id 
FROM projectdetails
WHERE recycle_bin != 1
AND creater_id = 7923;

答案 1 :(得分:1)

根据您的描述和示例输出,您似乎只想查看带有以下内容的记录:

  1. recycle_bin> 1
  2. 父树的最顶层节点。最顶层节点由具有当前节点的parent_id的项目是否与所讨论的creater_id一起确定。
  3. creater_id = 7923
  4. 因此,要获得这些条件,您需要三个过滤器:

     SELECT        CURRENT_ROW.PROJECT_ID
                  ,CURRENT_ROW.PROJECT_NAME
                  ,CURRENT_ROW.PARENT_ID
                  ,CURRENT_ROW.RECYCLE_BIN
                  ,CURRENT_ROW.CREATER_ID
    FROM          PROJECTDETAILS CURRENT_ROW
    WHERE         
          recycle_bin != 1
      AND creater_id=7923
      /* This will determine if the node is the topmost based on whether it has a parent */
      AND NOT EXISTS (
             select 1 
             FROM PROJECTDETAILS PARENT 
             where CURRENT_ROW.parent_id = PARENT.project_id
               and PARENT.creater_id = CURRENT_ROW.creater_id
               and PARENT.recycle_bin != 1
              );
    

    有关工作示例,请参阅此sqlfiddle

    这可以获得您正在寻找的确切输出。

答案 2 :(得分:1)

这样的事情? FIDDLE以供参考

SELECT project_id,project_name,parent_id,recycle_bin,creater_id
FROM projectdetails AS pd1
JOIN(
    SELECT parent_id FROM projectdetails as pd
    WHERE pd.parent_id > 0 AND pd.recycle_bin > 1
) AS t ON t.parent_id = pd1.project_id

使用新数据.. NEW_FIDDLE

SELECT 
  if(pd1.recycle_bin >1, pd1.project_id, t.project_id) AS project_id,
  if(pd1.recycle_bin >1, pd1.project_name, t.project_name) AS project_name,
  if(pd1.recycle_bin >1, pd1.parent_id, t.parent_id) AS parent_id,
  if(pd1.recycle_bin >1, pd1.recycle_bin, t.recycle_bin) AS recycle_bin,
  if(pd1.recycle_bin >1, pd1.creater_id, t.creater_id) AS creater_id
FROM projectdetails AS pd1
JOIN(
  SELECT * FROM projectdetails AS pd
  WHERE pd.parent_id > 0 
    AND pd.recycle_bin > 1
) AS t ON t.parent_id = pd1.project_id
GROUP BY project_id

不使用IF' LAST_FIDDLE

SELECT 
   pd1.project_id,
   pd1.project_name,
   pd1.parent_id,
   pd1.recycle_bin,
   pd1.creater_id
FROM projectdetails AS pd1
WHERE NOT EXISTS(
  SELECT pd.recycle_bin FROM projectdetails as pd
  WHERE pd1.parent_id = pd.project_id 
    AND pd.recycle_bin > 1
) AND pd1.creater_id = 7923 and pd1.recycle_bin > 1
GROUP BY pd1.project_id;