[英]MySQL SELECT only comments that belong to posts viewed on a specific page(pagination using LIMIT)
好吧,在嘗試解決10名初中生的問題之后,我不太確定如何解決該問題。
我有1個MySQL查詢,可從數據庫中提取所有10條最新帖子,並從posts表中將其顯示在博客頁面的第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,我找不到任何包含分頁因子的相關解決方案。
從第一個查詢中刪除除p.id
之外的所有內容, 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.