mysql - 显示用户关注相同 ID 的结果

标签 mysql sql

首先,对于问题的标题,我真的不知道我可以给它起什么标题。有什么建议吗?

无论如何,我正在创建一个新闻源类型的网站,显示您关注的网络和关注同一网络的用户的帖子和状态更新。

我当前的sql查询是

SELECT companyname,post_content,forename,surname,date,type,postertype
FROM cheddar.usernetworks un
LEFT JOIN cheddar.feed f ON f.posterid = un.networkid OR f.posterid = un.userid
LEFT JOIN cheddar.users u ON u.userID = f.posterid AND postertype = 'user' 
LEFT JOIN cheddar.networks n ON n.id = f.posterid AND postertype = 'network'
WHERE un.userid = '9'
ORDER BY date DESC 

enter image description here

正如我提到的,我的目标是显示网络本身的结果(帖子)以及与登录该站点的用户遵循相同网络的用户。 (我已将 9 作为测试查询中的用户 ID)。

目前它只显示来自用户 9 和网络的帖子,但没有来自用户 6 的帖子。

有什么想法吗?我已经尝试了 2 个小时的各种不同的东西,这是我想出的最后一件事。

用于提要表的 SQL

CREATE TABLE `feed` (
  `postid` int(11) NOT NULL AUTO_INCREMENT,
  `posterid` int(11) DEFAULT NULL,
  `post_content` mediumtext,
  `date` varchar(45) DEFAULT NULL,
  `type` varchar(45) DEFAULT NULL,
  `postertype` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`postid`)
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=latin1;

INSERT INTO `feed` (`postid`,`posterid`,`post_content`,`date`,`type`,`postertype`) VALUES (1,9,'hello! meow!','1516889612','update','user');
INSERT INTO `feed` (`postid`,`posterid`,`post_content`,`date`,`type`,`postertype`) VALUES (2,10293,'we are the peaky blinders','1516889612','update','network');
INSERT INTO `feed` (`postid`,`posterid`,`post_content`,`date`,`type`,`postertype`) VALUES (3,9,'Earned a new badge!','1516901430','badge','user');
INSERT INTO `feed` (`postid`,`posterid`,`post_content`,`date`,`type`,`postertype`) VALUES (4,9,'Top Seller','1516901430','topseller','user');
INSERT INTO `feed` (`postid`,`posterid`,`post_content`,`date`,`type`,`postertype`) VALUES (5,9,' dddd','1516909119','update','user');
INSERT INTO `feed` (`postid`,`posterid`,`post_content`,`date`,`type`,`postertype`) VALUES (6,9,' lol','1516909164','update','user');
INSERT INTO `feed` (`postid`,`posterid`,`post_content`,`date`,`type`,`postertype`) VALUES (7,9,' haha','1516909214','update','user');
INSERT INTO `feed` (`postid`,`posterid`,`post_content`,`date`,`type`,`postertype`) VALUES (8,9,'Why is GOD so great?','1516909574','question','user');
INSERT INTO `feed` (`postid`,`posterid`,`post_content`,`date`,`type`,`postertype`) VALUES (9,9,' Coffee is for closers!','1516909968','tip','user');
INSERT INTO `feed` (`postid`,`posterid`,`post_content`,`date`,`type`,`postertype`) VALUES (18,9,' lol','1516915928','update','user');
INSERT INTO `feed` (`postid`,`posterid`,`post_content`,`date`,`type`,`postertype`) VALUES (19,9,' meow','1516916436','update','user');
INSERT INTO `feed` (`postid`,`posterid`,`post_content`,`date`,`type`,`postertype`) VALUES (20,9,' Haha! Was that you?!','1516916487','update','user');
INSERT INTO `feed` (`postid`,`posterid`,`post_content`,`date`,`type`,`postertype`) VALUES (21,10295,'Sell wine! It\'s not a crime','1516916905','update','network');
INSERT INTO `feed` (`postid`,`posterid`,`post_content`,`date`,`type`,`postertype`) VALUES (22,42,'I love women and sales!','1516916905','update','user');
INSERT INTO `feed` (`postid`,`posterid`,`post_content`,`date`,`type`,`postertype`) VALUES (25,9,' is mo a bad boy','1516926061','question','user');

SQL FOR 用户网络

CREATE TABLE `usernetworks` (
  `id` int(11) NOT NULL,
  `userID` int(11) DEFAULT NULL,
  `networkid` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
*/
INSERT INTO `usernetworks` (`id`,`userID`,`networkid`) VALUES (1,9,10293);
INSERT INTO `usernetworks` (`id`,`userID`,`networkid`) VALUES (4,42,10293);
INSERT INTO `usernetworks` (`id`,`userID`,`networkid`) VALUES (5,3,10293);
INSERT INTO `usernetworks` (`id`,`userID`,`networkid`) VALUES (8,6,10295);

最佳答案

我想这就是你想要的:

SQL Fiddle

Results :

| id | userID | networkid |
|----|--------|-----------|
|  1 |      9 |     10293 |
|  4 |     42 |     10293 |
|  5 |      3 |     10293 |
|  8 |      6 |     10295 |

查询 2:

SELECT f.*, post_content,date,type,postertype

FROM usernetworks un
LEFT JOIN feed f ON f.posterid = un.networkid OR f.posterid = un.userid
WHERE un.userid in (
  select u1.userID 
  from usernetworks u1
  inner join usernetworks u2 on u1.networkid = u2.networkid
  where u2.userID = 9
)
ORDER BY date DESC 

Results :

| postid | posterid |              post_content |       date |      type | postertype |              post_content |       date |      type | postertype |
|--------|----------|---------------------------|------------|-----------|------------|---------------------------|------------|-----------|------------|
|     25 |        9 |           is mo a bad boy | 1516926061 |  question |       user |           is mo a bad boy | 1516926061 |  question |       user |
|     22 |       42 |   I love women and sales! | 1516916905 |    update |       user |   I love women and sales! | 1516916905 |    update |       user |
|     20 |        3 |      Haha! Was that you?! | 1516916487 |    update |       user |      Haha! Was that you?! | 1516916487 |    update |       user |
|     19 |        9 |                      meow | 1516916436 |    update |       user |                      meow | 1516916436 |    update |       user |
|     18 |        9 |                       lol | 1516915928 |    update |       user |                       lol | 1516915928 |    update |       user |
|      9 |        9 |    Coffee is for closers! | 1516909968 |       tip |       user |    Coffee is for closers! | 1516909968 |       tip |       user |
|      8 |        9 |      Why is GOD so great? | 1516909574 |  question |       user |      Why is GOD so great? | 1516909574 |  question |       user |
|      7 |        9 |                      haha | 1516909214 |    update |       user |                      haha | 1516909214 |    update |       user |
|      6 |        9 |                       lol | 1516909164 |    update |       user |                       lol | 1516909164 |    update |       user |
|      5 |        9 |                      dddd | 1516909119 |    update |       user |                      dddd | 1516909119 |    update |       user |
|      3 |        9 |       Earned a new badge! | 1516901430 |     badge |       user |       Earned a new badge! | 1516901430 |     badge |       user |
|      4 |        9 |                Top Seller | 1516901430 | topseller |       user |                Top Seller | 1516901430 | topseller |       user |
|      1 |        9 |              hello! meow! | 1516889612 |    update |       user |              hello! meow! | 1516889612 |    update |       user |
|      2 |    10293 | we are the peaky blinders | 1516889612 |    update |    network | we are the peaky blinders | 1516889612 |    update |    network |
|      2 |    10293 | we are the peaky blinders | 1516889612 |    update |    network | we are the peaky blinders | 1516889612 |    update |    network |
|      2 |    10293 | we are the peaky blinders | 1516889612 |    update |    network | we are the peaky blinders | 1516889612 |    update |    network |

关于mysql - 显示用户关注相同 ID 的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48453910/

相关文章:

mysql - 具有多条记录的案例

php - 向mysql中插入数据

mysql - 从组中选择最大元素列

python - 如何在 Django 查询中使用正则表达式

mysql - 从 LEFT JOIN 获取行数

php - 如何在 php 中的新页面上开始新报告

java - MySQL数据库查询和更新

sql - 按选择第一次出现的字段分组我怎样才能得到最后一个?

Mysql:Concat内部的连接符号与条件语句

mysql - SQL 查询 : Separate column where IDs are the same but type is different