2017-03-22 11 views
0

私はoperationoperationTaskの2つのテーブルを持っています。 :さんは状態 "1ブール0" を同じ外来キーを持つすべての行を選択して、条件を満たす。

  • operationだけ

    • ID

    operationTaskは "外部キーとして"

    • ID
    • operation_idをしていたとしましょう

    これらの2つのテーブルの関係は1対多です。

    私は彼らのすべてのタスクが "operationtask" ステータスは、私が試してみました何1.

    に等しいすべての操作を選択します:

    操作:

    例えば

    SELECT * 
    FROM `operation` 
    WHERE operation.id = All(
        SELECT task.operation_id 
        FROM operationtask task 
        WHERE task.status=1 
        GROUP BY task.operation_id) 
    

    ID 
    --- 
    1 
    2 
    3 
    

    操作タスク:

    ID operation_id status 
    --- ------------ ------ 
    1   1   1 
    2   1   0 
    3   2   1 
    4   2   1 
    5   3   0 
    6   3   0 
    

    結果は次のようになります。

    操作:

    ID 
    --- 
    2 
    
    +0

    あなたのコードの問題は何ですか? – Jens

    +0

    @Jensが正しい結果を返さなかった – Mohammad

    +0

    期待される結果と結果を持つサンプルデータを表示できますか? – Jens

    答えて

    1
    select * 
    from operations o 
    where not exists (
         select 1 
         from operationtask t 
         where t.operation_id = o.id and t.status = 0) 
    
    関連する問題