我目前在查询中遇到问题。我不知道为什么它不起作用。这是我的问题:
INSERT INTO tbl_leave_count (leave_count,leave_user_id,leave_id)
SELECT COUNT(*) as leaveCount, leave_user_id,leave_id
FROM tbl_leaverequest
WHERE leave_user_id=2 AND leave_id = 1
ON DUPLICATE KEY UPDATE leave_count = leaveCount;
但是当我将更新离开计数的值替换为数字时,它可以工作:
INSERT INTO tbl_leave_count (leave_count,leave_user_id,leave_id)
SELECT COUNT(*) as leaveCount, leave_user_id,leave_id
FROM tbl_leaverequest
WHERE leave_user_id=2 AND leave_id =1
ON DUPLICATE KEY UPDATE leave_count=5;
我想从查询中的select语句中获取值[COUNT(*)as leaveCount]。我该怎么办?
答案 0 :(得分:0)
列名应该相同
INSERT INTO tbl_leave_count (leave_count,leave_user_id,leave_id)
(SELECT COUNT(*) as leave_count, leave_user_id,leave_id FROM tbl_leaverequest WHERE leave_user_id=2 AND leave_id =1)
ON DUPLICATE KEY UPDATE leave_count=5;
答案 1 :(得分:0)
您可以尝试使用子查询:
$XmlFile = Get-Item($xmlFileName)
# Perform the XSD Validation
$readerSettings = New-Object -TypeName System.Xml.XmlReaderSettings
$readerSettings.Schemas.Add($compiledSchema)
$readerSettings.ValidationType = [System.Xml.ValidationType]::Schema
$readerSettings.ValidationFlags = [System.Xml.Schema.XmlSchemaValidationFlags]::ProcessInlineSchema -bor [System.Xml.Schema.XmlSchemaValidationFlags]::ProcessSchemaLocation
$readerSettings.add_ValidationEventHandler(
{
# Triggered each time an error is found in the XML file
Write-Host $("ERROR line $($_.exception.LineNumber) position $($_.exception.LinePosition) in '$xmlFileName': " + $_.Message) -ForegroundColor Red
$script:errorCount++
});
$reader = [System.Xml.XmlReader]::Create($XmlFile.FullName, $readerSettings)
while ($reader.Read()) { }
$reader.Close()
我认为您的原始INSERT INTO tbl_leave_count (leave_count, leave_user_id, leave_id)
SELECT leaveCount, leave_user_id, leave_id
FROM
(
SELECT (SELECT COUNT(*) FROM tbl_leaverequest
WHERE leave_user_id = 2 AND leave_id = 1) AS leaveCount,
leave_user_id,
leave_id
FROM tbl_leaverequest
WHERE leave_user_id = 2 AND leave_id = 1
) t
ON DUPLICATE KEY UPDATE leave_count = t.leaveCount;
存在问题,因为SELECT
出现非聚合列。考虑到COUNT(*)
子句中的条件,我使用子查询来查找tbl_leaverequest
的计数。