Zend Select NOT IN

后端 未结 1 417
被撕碎了的回忆
被撕碎了的回忆 2021-01-05 07:30

I have two tables with related data, and I want to select all the records from one table which do not exist in the other table, plus some other criteria on the related table

相关标签:
1条回答
  • 2021-01-05 08:15

    You can run direct sql, using $db->query(); yours would simply be:

    $results = $db->query("SELECT A.SOMETHING 
    FROM A 
    WHERE A.ID NOT IN (
        SELECT 
            B.TABLE_A_ID AS ID 
            FROM B 
            WHERE TABLE_C_ID = ?
    )", $id);
    

    EDIT: To answer whether this can be done with the object notation, yes:

    $sub_select = $zdb->select()
                      ->from("b", array("table_a_id AS id"))
                      ->where("table_c_id = ?", 'a');
    $select = $zdb->select()
                  ->from("a", array("something"))
                  ->where("id NOT IN ?", $sub_select);
    print $select->__toString();
    

    gives

    SELECT `a`.`something` FROM `a`
    WHERE (id NOT IN
        (SELECT `b`.`table_a_id` AS `id` FROM `b` WHERE (table_c_id = 'a')))
    
    0 讨论(0)
提交回复
热议问题