过滤具有多个条件的MySQL查询

时间:2015-08-05 11:13:52

标签: php mysql arrays conditional-statements notin

我的输出数组名为" Feed Items" (FI):

(
    [0] => Array
        (
            [reg] => 2015-08-03 13:39:00
            [id] => fd7ec4107d16b07c1a13cbdd386af8d2cb05ffca
            [user_id] => de5fd44db1760b006b1909cf1db11a78b38e455c
            [img] => edee88e88cf6e17732e393b5433cfd894662902e
            [type] => new_join_ambition
        )

)

我生成了一个名为" RemovedFeedItems" (rfi)具有以下结构:

(
    [0] => Array
        (
            [object_id] => fd7ec4107d16b07c1a13cbdd386af8d2cb05ffca
            [postee_id] => de5fd44db1760b006b1909cf1db11a78b38e455c
            [type] => new_join_ambition
        )

)

我的任务是获取具有以下条件的记录:

ri.id == rfi.object_id && ri.user_id == rfi.postee_id && ri.type == rfi.type

我用来获取Feed Items数组的查询是:

SELECT 
                i.registered AS reg, 
                a.id, 
                u.id AS user_id, 
                ui.image_id AS img,  
                'new_join_ambition' AS type, 
            FROM x_ambition_invites i 
                LEFT JOIN x_user u
                    ON i.to = u.id
                LEFT JOIN x_user_images ui 
                    ON u.id = ui.user_id
                LEFT JOIN x_ambitions a
                    ON i.ambition_id = a.id
            WHERE a.registered 
                BETWEEN '2014-07-21 14:25:03' AND '2015-08-05 12:04:41' 
            AND i.to != '8fa7a1679560876eaf2f8060abd916b692c719dc' 
            AND i.to IN ('de5fd44db1760b006b1909cf1db11a78b38e455c')

如何调整查询以实现从 FeedItems 数组中删除所述记录的条件?

提前致谢。

1 个答案:

答案 0 :(得分:1)

尝试使用内联表

SELECT ... FROM  ... WHERE (a,b,c) NOT IN (SELECT a1, b1, c1 UNION SELECT a2, b2, c2 UNION SELECT...)

缺点是您必须构建一个大型查询字符串。 : - (