我这里有一个表 SQL
| id | dataid | datarow | datacolumn | datavalue
_________________________________________________
| 1 | 1 | 1 | FirstName | John
| 1 | 1 | 1 | LastName | Lobo
| 1 | 1 | 1 | Age | 35
| 1 | 1 | 2 | FirstName | Mich
| 1 | 1 | 2 | LastName | Handness
| 1 | 1 | 2 | Age | 22
| 1 | 1 | 3 | FirstName | Mike
| 1 | 1 | 3 | LastName | Longbow
| 1 | 1 | 3 | Age | 55
哪个会输出这样的东西
FirstName LastName Age
John Lobo 35
Mich Handness 22
Mike Longbow 55
SELECT DISTINCT
t1.datavalue AS Firstname,
t2.datavalue AS Lastname,
t3.datavalue AS Age
FROM largedatatable t1
LEFT JOIN (SELECT datavalue,datarow FROM largedatatable WHERE datacolumn='LastName') t2 ON t1.datarow = t2.datarow
LEFT JOIN (SELECT datavalue,datarow FROM largedatatable WHERE datacolumn='Age') t3 ON t1.datarow = t3.datarow
WHERE t1.dataid = 1
这给了我这样的结果
Firstname LastName Age
John Lobo 35
Mich Handness 22
Mike Longbow 55
现在脚本工作得很好;但是如果它的数据集很大,查询会很慢。无论如何我仍然可以优化这个 SQL 查询。谢谢!