我的数据库中有以下表格,我无法更改或修改。我使Log
表格保持简单,但LogDetail
与我的数据库中的相同。
日志表
Id User Department Service Method
21 John Sales UserService GetUser
日志明细表
Id LogRef ParamName ParamValue
30 21 FirstName Adam
31 21 LastName Smith
32 21 Age 35
33 21 Gender M
现在,我正在使用以下查询来获取搜索者(Adam,Smith,35,M)
SELECT
L.*, D1.ParamName, D2.ParamName, D3.ParamName, D4.ParamName
FROM Log as L
INNER JOIN LogDetail as D1 on L.Id = D1.LogRef
INNER JOIN LogDetail as D2 on L.Id = D2.LogRef
INNER JOIN LogDetail as D3 on L.Id = D3.LogRef
INNER JOIN LogDetail as D4 on L.Id = D4.LogRef
WHERE
D1.ParamName='FirstName' and D1.ParamValue='Adam' and
D2.ParamName='LastName' and D2.ParamValue='Smith' and
D3.ParamName='Age' and D3.ParamValue=35 and
D4.ParamName='Gender' and D4.ParamValue='M'
有一个更好的方法吗?