Php 为不同的字段ID和数据查询选择数据
我有这张桌子Php 为不同的字段ID和数据查询选择数据,php,mysql,phpmyadmin,Php,Mysql,Phpmyadmin,我有这张桌子 userid fieldid data 1 2 123123 1 3 name 1 4 something 如何为具有不同别名的fieldid 2和3选择数据 从inf\u数据中选择数据作为nrew,其中fieldid='1'一种方法使用条件聚合: select id, m
userid fieldid data
1 2 123123
1 3 name
1 4 something
如何为具有不同别名的fieldid 2和3选择数据
从inf\u数据中选择数据作为nrew,其中fieldid='1'
一种方法使用条件聚合:
select id,
max(case when fieldid = 2 then data end) as field2,
max(case when fieldid = 4 then data end) as field4
from t
group by id;
一种方法使用条件聚合:
select id,
max(case when fieldid = 2 then data end) as field2,
max(case when fieldid = 4 then data end) as field4
from t
group by id;
你可以加入
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'
你可以加入
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'
你能显示你想要的结果集吗?@GordonLinoff我已将我的问题选择数据更新为来自inf_数据的nrew,其中fieldid=2或fieldid=3@lakhvirkumar我想在单独的专栏中获得结果。我没有得到你想要的东西。你能在结果集中显示你想要的吗?@GordonLinoff我已将我的问题选择数据更新为nrew来自inf_数据,其中fieldid=2或fieldid=3@lakhvirkumar我想在分栏中得到的结果没有得到你想要什么