列等于同一列时在同一表中获取值的最佳方法

时间:2018-11-08 14:26:34

标签: mysql mysqli

我有db表用户,该表有5行

id username attr   value
1   ahmed   text1   ....
2   ahmed   text2   ....
3   ahmed   text3   ....
....

我的查询是

select 
u1.value as `val1`,
u2.`value` as `val2` ,
u2.`value` as `val3` 
FROM `users` 
`u1` 
LEFT JOIN `users` `u2` ON (`u2`.`username` = `u1`.`username` AND `u2`.`attr` = 'text2')
LEFT JOIN `users` `u3` ON (`u3`.`username` = `u1`.`username` AND `u3`.`attr` = 'text3')
WHERE `u1`.`attr` = 'text1'

我希望任何想法都能使行速更快,对不起我的英语

0 个答案:

没有答案
相关问题