Mysql选择用户friendes喜欢

时间:2017-08-31 07:47:43

标签: mysql

我有这些表

帖子表

     object filename = s; // s is a string path which I get from database 
                Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
                Microsoft.Office.Interop.Word.Document doc1 = app.Documents.Open(s);
                object missing = System.Reflection.Missing.Value;
                app.Visible = true;

连接表

CREATE TABLE `users_posts` (
    `pid` INT(11) NOT NULL AUTO_INCREMENT, /*post id*/
    `uid` INT(11) NOT NULL, /*user who created the post, owner*/
    `created_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_date` DATETIME NOT NULL,
    `post_status` TINYINT(1) NOT NULL,
    `content` TEXT NULL COLLATE 'utf8_general_ci',
    PRIMARY KEY (`pid`),
)

喜欢桌子

CREATE TABLE `users_connections` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `user_1_uid` INT(11) NOT NULL, /*user id*/
    `user_2_uid` INT(11) NOT NULL, /*friend user id of user id*/
    `connections_status` TINYINT(4) NOT NULL,
    PRIMARY KEY (`id`)
)

我需要选择所有帖子和查询内部我需要检查我的朋友是否有喜欢我正在选择的帖子,如果是真的,我可以在其他帖子上显示此帖子以显示更多相关内容

CREATE TABLE `post_ups` (
    `upid` INT(11) NOT NULL AUTO_INCREMENT,
    `uid` INT(11) NOT NULL, /*user id who like the post*/
    `puid` INT(11) NOT NULL, /*post owner user id=users_posts.uid*/
    `pid` INT(11) NOT NULL, /*post id=users_posts.pid*/
    `up_status` TINYINT(1) NOT NULL,
    PRIMARY KEY (`upid`),
    INDEX `uid` (`uid`),
    INDEX `up_status` (`up_status`),
    INDEX `pid` (`pid`)
)

这可能吗?

1 个答案:

答案 0 :(得分:0)

查询必须如下:

SELECT up.*, 
    (SELECT count(*) FROM post_ups pups 
      WHERE up.pid=pups.pid AND  up.uid <> pups.uid/*exclude own like*/
        AND EXISTS (SELECT 0 FROM users_connections uc 
                            WHERE uc.user_1_uid=up.uid/*pups.puid*/ AND uc.user_2_uid=pups.uid)) relevance
  FROM users_posts up
ORDER BY relevance DESC;