[英]Trouble with SQL join, where, having clause
我在理解如何進行查詢時會遇到麻煩(狀態:200 OK),這將向我顯示“三大最受歡迎的文章”。
我目前正在處理2張桌子。
這些表中的列:表“ public.log”
Column | Type | Modifiers
--------+--------------------------+--------------------------------------------------
path | text |
ip | inet |
method | text |
status | text |
time | timestamp with time zone | default now()
id | integer | not null default nextval('log_id_seq'::regclass)
索引:
和
Table "public.articles"
Column | Type | Modifiers
--------+--------------------------+-------------------------------------------------------
author | integer | not null
title | text | not null
slug | text | not null
lead | text |
body | text |
time | timestamp with time zone | default now()
id | integer | not null default nextval('articles_id_seq'::regclass)
索引:
。
到目前為止,我已經根據我的水平和對SQL的理解編寫了此查詢。
SELECT articles.title, log.status
FROM articles join log
WHERE articles.title = log.path
HAVING status = “200 OK”
GROUP BY title, status
顯然,這是不正確的。 我希望能夠從數據庫中提取三篇最受歡迎的文章,並且我知道將200 OK與“文章標題”進行“匹配”對我來說將顯示或計數一次“觀看”或點擊。 我的思考過程就像,我需要通過創建查詢來確定article.title = log.path(1個唯一值)在日志數據庫中顯示的次數(狀態為200 OK)。 我的任務實際上是編寫一個程序,該程序將使用“ [我的代碼獲取]數據庫”來打印結果,以通過使用連接,聚合和where子句來進行繁重的工作。.在Python代碼中進行最少的“后處理”本身。”
所有StackOverflow的任何解釋,想法,技巧都值得贊賞...
可能是以下幾點:
SELECT
a.title,
COUNT(*) AS cnt
FROM articles a
INNER JOIN log l
ON a.title = l.path
WHERE
l.lstatus = '200 OK'
GROUP BY
a.title
ORDER BY
COUNT(*) DESC
LIMIT 3;
這將返回狀態數200的最高狀態的三個文章標題。 該答案假定您正在使用MySQL。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.