首先,抱歉问题标题,我真的不知道我能说什么。有什么建议吗?
无论如何,我正在创建一个新闻源类型的网站,显示您关注的网络和遵循相同网络的用户的帖子和状态更新。
我当前的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
的帖子。
有什么想法吗?我现在已经尝试了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 usernetworks
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);
答案 0 :(得分:1)
我认为这就是你想要的:
<强> 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 |