我有这张桌子

userid       fieldid            data
1              2               123123
1              3                name
1              4              something


我如何为具有不同别名的fieldid 2和3选择数据?

SELECT data AS nrew FROM inf_data where fieldid ='1' <
我想得到:

userid      nrEw(fieldid=2)             smth(fieldid=4)
1            123123                        something

最佳答案

您可以使用加入

select i1.data as nrew, i2.data as smth from inf_data i1 join inf_data i2 on i1.userid=i2.userid where i1.fieldid='1' and i2.fieldid='4'


如果不确定是否需要“ on i1.userid = i2.userid”,则可以添加另一个where语句:

select i1.data as nrew, i2.data as smth from inf_data i1 join inf_data i2  where i1.fieldid='1' and i2.fieldid='4' and i1.userid='1' and i2.userid='1'

关于php - 查询选择数据以获取不同的fieldid和数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43978820/

10-11 09:06