我使用MySQL,并且我有一个代码可以从每个类别中获取最后6篇文章:
select a.*
from article a
where a.created >= coalesce((select a2.created
from article a2
where a2.category = a.category
order by a2.created desc
limit 5, 1
), a.created
);
现在,我还需要从另一个表中获取每篇文章的总视图。怎么做?这不起作用:
select a.*, Count(view.*) as CountViews
from article a
where a.created >= coalesce((select a2.created
from article a2
where a2.category = a.category
order by a2.created desc
limit 5, 1
), a.created
) left join view on a.id = view.post_id;
按类别打印文章的示例:https://nedvigimostmsk.ru/
答案 0 :(得分:2)
由于Gordon现在似乎已离线,因此我将在此处发布我的编辑内容作为另一个答案。这应该给您所需的订单。
select a.*,
(select count(*)
from views v
where a.id = v.post_id
) as num_views
from article a
where a.created >= coalesce((select a2.created
from article a2
where a2.category = a.category
order by a2.created desc
limit 5, 1
), a.created
)
ORDER BY a.created DESC;
答案 1 :(得分:1)
我可能建议使用子查询:
[CmdletBinding()]
param()
Trace-VstsEnteringInvocation $MyInvocation
$appdirectory = get-vstsinput -Name appdirectory
$webappname = get-vstsinput -Name webappname
$ResourceGroupName = get-vstsinput -Name ResourceGroupName
#write-host $appdirectory
$location="East US"
try {
# Get inputs.
# Get publishing profile for the web app
$xml = [xml](Get-AzureRmWebAppPublishingProfile -Name $webappname `
-ResourceGroupName $ResourceGroupName `
-OutputFile null)
# Extracts connection information from publishing profile
$username = $xml.SelectNodes("//publishProfile[@publishMethod=`"FTP`"]/@userName").value
$password = $xml.SelectNodes("//publishProfile[@publishMethod=`"FTP`"]/@userPWD").value
$url = $xml.SelectNodes("//publishProfile[@publishMethod=`"FTP`"]/@publishUrl").value
Write-Output "$username"
Write-Output "$password"
Write-Output "$url"
#Write-Output "$localpath"
# Upload files recursively
Set-Location $appdirectory
$webclient = New-Object -TypeName System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($username,$password)
$files = Get-ChildItem -Path $appdirectory -Recurse | Where-Object{!($_.PSIsContainer)}
foreach ($file in $files)
{
$relativepath = (Resolve-Path -Path $file.FullName -Relative).Replace(".\", "").Replace('\', '/')
$uri = New-Object System.Uri("$url/$relativepath")
"Uploading to " + $uri.AbsoluteUri
$webclient.UploadFile($uri, $file.FullName)
}
$webclient.Dispose()
} finally {
Trace-VstsLeavingInvocation $MyInvocation
}
查询的问题是您使用的 {
"id": "LongStringofNumbers",
"name": "NameOfTask",
"friendlyName": "FriendlyNameOfTask",
"description": "Build Task that will upload a file or folder to a destination in Azure using Published Profile Credentials.",
"helpMarkDown": "",
"category": "Build",
"visibility": [
"Build"
],
"runsOn": [
"Agent",
"DeploymentGroup"
],
"author": "ME",
"version": {
"Major": 0,
"Minor": 0,
"Patch": 16
},
"instanceNameFormat": "Uploads File Using Published Profile Credentials",
"groups": [
{
"name": "advanced",
"displayName": "Advanced",
"isExpanded": true
}
],
"inputs": [
{
"name": "appdirectory",
"type": "filePath",
"label": "Source Path",
"defaultValue": "",
"required": true,
"helpMarkDown": "Location of file(s) for uploading to Azure."
},
{
"name": "webappname",
"type": "string",
"label": "Azure Webapp name",
"defaultValue": "",
"required": true,
"helpMarkDown": "Azure App name; I.E. - 900014campuslinkapi."
},
{
"name": "ResourceGroupName",
"type": "string",
"label": "Azure Resource Group name",
"defaultValue": "",
"required": true,
"helpMarkDown": "Azure Resource Group Name I.E. - 900014-prod."
}
],
"execution": {
"PowerShell3": {
"target": "powershell.ps1",
"platforms": [
"windows"
],
"argumentFormat": "",
"workingDirectory": "$(currentDirectory)"
}
}
}
没有select a.*,
(select count(*)
from views v
where a.id = v.post_id
) as num_views
from article a
where a.created >= coalesce((select a2.created
from article a2
where a2.category = a.category
order by a2.created desc
limit 5, 1
), a.created
);
。要么返回错误(在最新版本的MySQL中使用默认设置),要么返回一个神秘的行。您可以使用COUNT()
来解决此问题,但是相关的子查询通常具有更好的性能-尤其是使用正确的索引。
答案 2 :(得分:0)
作为Gordon的替代方案,您还可以对嵌套查询进行联接,该查询从views
表开始计数和分组
select a.*, v.cnt as total_views
from article a
inner join
(
select post_id, count(*) as cnt from views group by post_id
) v on a .id = v.post_id
where a.created >= coalesce((select a2.created
from article a2
where a2.category = a.category
order by a2.created desc
limit 5, 1
), a.created
);
答案 3 :(得分:0)
我会尝试
... )left join view on a.id = view.post_id
GROUP BY a.*
此外,在您的SELECT语句中,使用它代替COUNT(view。*)AS CountViews
COUNT(view.post_id) AS CountViews
一般来说,最好列出要返回的特定列,而不是仅使用SELECT a.*