Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql Sshow显示用户使用相同id的结果_Mysql_Sql - Fatal编程技术网

Mysql Sshow显示用户使用相同id的结果

Mysql Sshow显示用户使用相同id的结果,mysql,sql,Mysql,Sql,首先,很抱歉问题的标题,我真的不知道我能给这个标题取什么。有什么建议吗 不管怎么说,我正在创建一个类似于newsfeed的网站,通过你关注的网络和关注相同网络的用户来显示帖子和状态更新 我当前的sql查询是 SELECT companyname,post_content,forename,surname,date,type,postertype FROM cheddar.usernetworks un LEFT JOIN cheddar.feed f ON f.posterid = un.net

首先,很抱歉问题的标题,我真的不知道我能给这个标题取什么。有什么建议吗

不管怎么说,我正在创建一个类似于newsfeed的网站,通过你关注的网络和关注相同网络的用户来显示帖子和状态更新

我当前的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 

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

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

有什么想法吗?我已经试了两个小时了,这是我最后想到的

提要表的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

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);

我想这就是你想要的:

| id | userID | networkid |
|----|--------|-----------|
|  1 |      9 |     10293 |
|  4 |     42 |     10293 |
|  5 |      3 |     10293 |
|  8 |      6 |     10295 |
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 
| 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 |
查询2

| id | userID | networkid |
|----|--------|-----------|
|  1 |      9 |     10293 |
|  4 |     42 |     10293 |
|  5 |      3 |     10293 |
|  8 |      6 |     10295 |
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 
| 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 |

| id | userID | networkid |
|----|--------|-----------|
|  1 |      9 |     10293 |
|  4 |     42 |     10293 |
|  5 |      3 |     10293 |
|  8 |      6 |     10295 |
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 
| 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 |

请参阅提供创建表和一些数据请使用提要数据更新问题。希望这是足够的谢谢你@blag。我从中学到了很多。谢谢。@ScottO'connor不客气;)您可以尝试重构查询以仅使用内部联接,而不是子查询中的