使用mysql" IN"运算符在这种查询中

时间:2014-04-14 02:55:51

标签: php mysql sql

我在下面的查询中使用了union。

"SELECT * FROM (
            SELECT 1 AS `table`, 
            `comment_post_id` AS `feed_id`, 
            `blog_id` AS `from_blog`, 
            `comment_author` AS `author`, 
            `comment_content_stripped` AS `feed_title`, 
            `comment_content` AS `post_content_s`, 
            `type` AS `type`, 
            null AS `object_type`,
            `comment_date_gmt` AS `date` 
            FROM `wp_site_comments`
            UNION
            SELECT 2 AS `table`, 
            `post_id` AS `feed_id`, 
            null AS `from_blog`,  
            `blog_id` AS `author`, 
            `post_title` AS `feed_title`, 
            `post_content_stripped` AS `post_content_s`, 
            `post_type` AS `type`, 
            null AS `object_type`,
            `post_published_gmt` AS `date` 
            FROM `wp_site_posts`
        UNION
        SELECT 3 AS `table`, 
            `object_id` AS `feed_id`, 
            `blog_id` AS `from_blog`,  
            `user_id` AS `author`, 
            null AS `feed_title`, 
            null AS `post_content_s`, 
            `type` AS `type`, 
            `object_type` AS `object_type`,
            `date_added` AS `date` 
            FROM `wp_global_likes`
        UNION
        SELECT 4 AS `table`, 
            `object_id` AS `feed_id`, 
            null AS `from_blog`,  
            `user_id` AS `author`, 
            null AS `feed_title`, 
            null AS `post_content_s`, 
            `type` AS `type`, 
            `object_type` AS `object_type`,
            `date_added` AS `date` 
            FROM `wp_global_followers`
        ) AS tb
        ORDER BY `date` DESC"

基本上我只想选择author以逗号分隔值的行,如下所示:

例如。 $blog_ids = (23, 55, 19, 10)$user_ids = (22, 55, 19, 40)

联合中的第一个表,authorcomment_author,它是用户ID。

联合中的第二个表,author is blog_id`是一个博客ID。

联合的第三个和第四个表,authoruser_id,它是user_id。

现在,我希望以某种方式将author与博客ID和用户ID区分开,以便我的查询将选择author位于$blog_ids$user_ids的行。< / p>

我用过,

WHERE author in (" . $blog_ids . "),它返回正确。现在我想要加入$user_id

请注意,$blog_ids$user_ids可能具有相同的值。

我希望你明白我的意思,我想这是我能做出的最佳解释。

0 个答案:

没有答案