好的,所以在试图解决10个青少年的问题后,我不太清楚如何解决它。
我有1个MySQL查询从数据库中提取所有10个最新帖子,并在帖子表中显示在博客页面的第0页上。我使用LIMIT和偏移量,这样PHP每页只能显示10个帖子。 这很有用:
$sql = "
SELECT u.name
, u.avatar
, p.id
, p.user_id
, p.title
, p.article
, DATE_FORMAT(p.set_in,'%d/%m/%Y') date
, DATE_FORMAT(p.last_edited,'%d/%m/%Y->%H:%i') edited
, p.genre
, p.band_image
, p.albums
FROM posts p
JOIN users u
ON u.id = p.user_id
ORDER
BY p.set_in DESC
LIMIT $offset, $rec_limit
";
现在我希望MySQL只选择属于我提供的帖子的评论,而不是将所有评论和使用PHP匹配的帖子与帖子的相关ID相匹配,但我似乎无法破解由于某种原因它失败了。 基于可以删除帖子的事实,我不能使用ID范围的一些静态逻辑。
现在这是我的查询,但这是一个非常糟糕的问题,并不适用于更大的网站:
$sql3 = "SELECT u.name,u.avatar,p.id,p.user_id,DATE_FORMAT(c.post_date,'%d/%m/%Y->%H:%i') date, c.ctitle, c.comment, c.id, c.user_id, c.post_id cid FROM comments c "
. " JOIN users u ON u.id = c.user_id "
. " JOIN posts p ON c.post_id = p.id "
. " ORDER BY c.post_date DESC";
这是评论表:
id post_id user_id ctitle comment post_date
1 40 1 hinew hinew 2017-08-08 00:41:10
3 40 1 Another Comment test comment 2017-08-08 00:49:15
4 45 20 How are you fine 2017-08-08 01:06:30
5 1 1 aftersomefix yeahhhh 2017-08-08 01:14:12
6 1 26 dsfgdfg dfgdfg 2017-08-08 01:14:58
7 45 1 TEST TEST 2017-08-08 20:42:38
这是帖子表:
id user_id title article set_in last_edited genre band_image albums
1 1 Demo Text This is the first post demo text 2017-08-05 18:40:55 2017-08-07 21:17:04 yoyo 2017.08.07.20.17.04-02aacec988bf439c277e2a2b611a23... popo
18 19 This is a test test 2017-08-06 18:43:57 2017-08-07 21:18:11 another test 2017.08.07.20.18.11-177120fbd1a8951e2a70907f5600e5... wow this is a test
19 20 This is a 3ed TEST 2017-08-06 18:49:10 2017-08-07 21:19:14 So many test moses 2017.08.07.20.19.14-17362774_10154643389487979_824... yes i know we have many tests
38 1 rtyrtyrty asdasd 2017-08-07 15:19:57 2017-08-07 21:14:56 Metalcore 2017.08.07.20.14.56-17098309_963035173797767_82275... aert
jhkghjk
aertaert
39 1 Dude Yes I think it does! 2017-08-07 19:54:21 2017-08-07 21:11:47 Is this actually working? 2017.08.07.19.00.53-81c0e26d68e0d29ceb619758e164da... hi
40 1 Test albums This is a decp 2017-08-07 20:16:26 2017-08-07 21:11:23 albums genre 2017.08.07.20.01.50-tenor.gif one,two,tree,four,five,789789,lol
41 1 BAND EX 1 DESC EX 1 2017-08-08 00:27:45 0000-00-00 00:00:00 GENRE EX 1 2017.08.07.23.27.45-19399240_10158895409495261_149... EX 1
42 1 Band ex2 Desc ex 2 2017-08-08 00:28:19 0000-00-00 00:00:00 Genre ex2 2017.08.07.23.28.19-13240136_1004564029622092_8185... ex 2
43 1 Name 3 Desc 3 2017-08-08 00:28:52 0000-00-00 00:00:00 Genre 3 2017.08.07.23.28.52-reptilian-death-the-dawn-of-co... 3
44 1 Name 4 4 2017-08-08 00:29:15 0000-00-00 00:00:00 4 2017.08.07.23.29.15-Best Melodic Death Metal Cover... 4
45 1 WHAT hehe 2017-08-08 00:31:52 0000-00-00 00:00:00 MURDER 2017.08.07.23.31.52-Best Melodic Death Metal Cover... cool
我试图制作一个对我不起作用的WHERE IN,并且作为SQL noobie我找不到包含分页因素的任何相关解决方案。
答案 0 :(得分:0)
从第一个查询中删除p.id
以外的所有内容,并将其用作子查询。然后将其与评论表一起加入:
SELECT c.*, u.* -- select the columns you need
FROM comments c
JOIN users u ON u.id = c.user_id
JOIN (
SELECT p.id -- other columns if needed in the outer query
FROM posts p
ORDER BY p.set_in DESC
LIMIT $offset, $rec_limit
) p ON p.id = c.post_id