2015-09-18 71 views
1

我在我的數據庫中有一個「users」表,不幸的是裏面有很多亂七八糟的東西,我試着將它移到新的一個。但只是確切的事情不復制/粘貼整個。MySQL SELECT查詢WHERE「東西是真的」

下面是實例Db的樣子:

-------*/*------------*/*------------*/*------------*/*------------*/*------------ 
     id  level   name   kind   status   parentId 
    -------*/*------------*/*------------*/*------------*/*----------*/*------------ 
    EMD123F |  2  | OrgName1 |  Org  |   | rootID 
    --------------------------------------------------------------------------------- 
    DHAD781 |  3  | UserName1 | Person | active | EMD123F 
    --------------------------------------------------------------------------------- 
    7AJIZU7 |  3  | UserName2 | Person | active | EMD123F 
    --------------------------------------------------------------------------------- 
    DME123F |  2  | OrgName2 |  Org  |   | rootID 
    --------------------------------------------------------------------------------- 
    TT5451AL|  3  | UserName3 | Person | active | DME123F 
    --------------------------------------------------------------------------------- 
    RRMI7481|  2  | OrgName3 |  Org  |   | rootID 
    --------------------------------------------------------------------------------- 
    PPUNSAD9|  2  | OrgName4 |  Org  |   | rootID 
    --------------------------------------------------------------------------------- 
    GJASDNZB|  3  | UserName4 | Person | inactive | PPUNSAD9 
    --------------------------------------------------------------------------------- 
    KJNSCZM7|  2  | OrgName5 |  Org  |   | rootID 
    --------------------------------------------------------------------------------- 
    1UZGOPAS|  3  | UserName5 | Person | deleted | KJNSCZM7 
    --------------------------------------------------------------------------------- 

你在這裏看到什麼,有一些「企業」,它有0用戶等都是absolutley沒用,還有一些組織具有用戶,但他們不活動(不活動,刪除...)。

我的問題是如何編寫一個select語句來獲得所有組織在哪裏有一個活躍的人在裏面。

RealData是一個比較複雜一點,但我嘗試這樣:

UPDATE users set org_status=1 WHERE (select count(STATUS) FROM users WHERE users.MEMBERKIND="Person" AND users.STATUS="Active" AND users.ROOTORGANIZATIONUSERID= users.ROOTORGANIZATIONIDCORRECTED)>0 AND MEMBERKIND = "Organization" AND LEVEL=2 

創建新行「org_status」,並將其設置爲NULL之後,我嘗試更新它

+0

請提供您到目前爲止嘗試過的查詢/查詢。但一個子選擇應該工作,我猜(未經測試):'SELECT * FROM orgaTable WHERE(從orgaTable AS用戶選擇count(*)WHERE users.parentId = orgaTable.id)> 0;' –

+0

可能的重複[如何創建MySQL分層遞歸查詢](http://stackoverflow.com/questions/20215744/how-to-create-a-mysql-hierarchical-recursive-query) – vhu

回答

2
select t1.* 
from your_table t1 
join your_table t2 on t2.parentId = t1.id 
where t2.status = 'active' 
+0

嘿,我很抱歉,如果我混淆你,但沒有2個表,這只是一個atm – Andurit

+0

是的。我在查詢中加入同一個表('your_table')兩次,使用不同的別名't1'和't2' –

+0

嘿,謝謝你的觀點,我多次嘗試它,很不幸,我無法獲得成功的結果。我只有24行休息幾千失去。 Logicaly這看起來不錯,所以我不知道哪裏是aproblem – Andurit

1

假設您只關心屬於沒有中間級別的組織的人員,則可以使用組織exists

select o.* 
from users o 
where exists (select 1 
       from users p 
       where p.parentid = o.id and 
        o.kind = 'Org' and 
        p.kind = 'Person' and 
        p.status = 'active' 
      ); 
+0

嘿,我很抱歉,如果我混淆你,但沒有2個表,它只是一個atm – Andurit

+0

@Andurit。 。 。此查詢僅涉及一個表。 –