我正在使用 Viescas 的SQL Queries for Mere Mortals及其資料集。
如果我運行他的代碼:
select customers.CustFirstName || " " || customers.CustLastName as "Name",
customers.CustStreetAddress || "," || customers.CustZipCode || "," || customers.CustState as "Address",
count(engagements.EntertainerID) as "Number of Contracts",
sum(engagements.ContractPrice) as "Total Price",
max(engagements.ContractPrice) as "Max Price"
from customers
inner join engagements
on customers.CustomerID = engagements.CustomerID
group by customers.CustFirstName, customers.custlastname,
customers.CustStreetAddress,customers.CustState,customers.CustZipCode
order by customers.CustFirstName, customers.custlastname;
我得到類似下表的資訊:
名稱 地址 合約數量 總價 最高價
0 1 7 8255.00 2210.00
0 1 11 11800.00 2570.00
0 1 10 12320.00 2450.00
0 1 8 10795.00 2750.00
0 1 8 25585.00 14105.00
0 1 6 7560.00 2300.00
但是,第一行應該在 Name 列中輸出 Carol Viescas... 為什么我們得到的是零呢?
uj5u.com熱心網友回復:
該運算子||是MySql 中的邏輯 OR運算子(自 8.0.17 版起已棄用),而不是連接運算子。
因此,當您將它與字串一起用作運算元時,MySql 會將字串隱式轉換為數字(有關詳細資訊,請查看運算式評估中的型別轉換),0其結果是任何不以數字開頭的字串,最終結果為0( = false)。
其結果也可能是1(= true)如果任何字串與非零數字部分開頭,就像(我懷疑)與列的情況下CustZipCode,你會得到1的"Address"。
如果你想連接字串,你應該使用函式CONCAT():
select CONCAT(customers.CustFirstName, customers.CustLastName) as "Name",
CONCAT(customers.CustStreetAddress, customers.CustZipCode, customers.CustState) as "Address",
......................................
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/334333.html
