Php concat列名称取决于值
我需要根据行值指定列名。以下是我的表格格式:- 表名=tbl\U占用率 因此,从这个表中,如果它们的值是1,我需要在下面的列中加上:-Php concat列名称取决于值,php,mysql,sql,codeigniter,Php,Mysql,Sql,Codeigniter,我需要根据行值指定列名。以下是我的表格格式:- 表名=tbl\U占用率 因此,从这个表中,如果它们的值是1,我需要在下面的列中加上:- 劳动人民 业主占用 学生 dss_转介 地方政府 输出格式如下:- Working People/Owner Occupied/Students/Dss Referrals/Local Authority when all of these columns value are one. 假设如果工作人员和dss推荐=1,那么我的输出将如下:- Workin
Working People/Owner Occupied/Students/Dss Referrals/Local Authority
when all of these columns value are one.
假设如果工作人员和dss推荐=1,那么我的输出将如下:-
Working People/Dss Referrals
试了一天。到目前为止还没有找到任何解决办法
我需要将此输出放在上面提到的一行中。因为此查询将作为“我的报告”模块中的子查询使用。该输出将在xls表中显示为一列。
请提供任何帮助。您可以在
CONCAT()函数中使用CASE
SELECT
DISTINCT risk_reference,
CONCAT(
CASE WHEN `working_people` = 1 THEN 'Working People /' ELSE '' END ,
CASE WHEN `owner_occupied` = 1 THEN 'Owner Occupied /' ELSE '' END ,
CASE WHEN `students` = 1 THEN 'Students /' ELSE '' END ,
CASE WHEN `dss_referrals` = 1 THEN 'Dss Referrals/' ELSE '' END ,
CASE WHEN `local_authority` = 1 THEN 'Local Authority' ELSE '' END
) `concat_columns`
FROM
`table`
编辑:
您可以使用DISTINCT
来获得不同的结果,但是在您的表中有多行,因此将有多个结果,尽管您可以使用groupby risk\u reference
,但是如果没有聚合函数,就不好使用CASE
您可以在CONCAT()函数中使用CASE
SELECT
DISTINCT risk_reference,
CONCAT(
CASE WHEN `working_people` = 1 THEN 'Working People /' ELSE '' END ,
CASE WHEN `owner_occupied` = 1 THEN 'Owner Occupied /' ELSE '' END ,
CASE WHEN `students` = 1 THEN 'Students /' ELSE '' END ,
CASE WHEN `dss_referrals` = 1 THEN 'Dss Referrals/' ELSE '' END ,
CASE WHEN `local_authority` = 1 THEN 'Local Authority' ELSE '' END
) `concat_columns`
FROM
`table`
编辑:
您可以使用DISTINCT
来获得不同的结果,但是在您的表中有多行,因此将有多个结果,尽管您可以使用groupby risk\u reference
,但是如果没有聚合函数,就不好使用CASE
您可以在CONCAT()函数中使用CASE
SELECT
DISTINCT risk_reference,
CONCAT(
CASE WHEN `working_people` = 1 THEN 'Working People /' ELSE '' END ,
CASE WHEN `owner_occupied` = 1 THEN 'Owner Occupied /' ELSE '' END ,
CASE WHEN `students` = 1 THEN 'Students /' ELSE '' END ,
CASE WHEN `dss_referrals` = 1 THEN 'Dss Referrals/' ELSE '' END ,
CASE WHEN `local_authority` = 1 THEN 'Local Authority' ELSE '' END
) `concat_columns`
FROM
`table`
编辑:
您可以使用DISTINCT
来获得不同的结果,但是在您的表中有多行,因此将有多个结果,尽管您可以使用groupby risk\u reference
,但是如果没有聚合函数,就不好使用CASE
您可以在CONCAT()函数中使用CASE
SELECT
DISTINCT risk_reference,
CONCAT(
CASE WHEN `working_people` = 1 THEN 'Working People /' ELSE '' END ,
CASE WHEN `owner_occupied` = 1 THEN 'Owner Occupied /' ELSE '' END ,
CASE WHEN `students` = 1 THEN 'Students /' ELSE '' END ,
CASE WHEN `dss_referrals` = 1 THEN 'Dss Referrals/' ELSE '' END ,
CASE WHEN `local_authority` = 1 THEN 'Local Authority' ELSE '' END
) `concat_columns`
FROM
`table`
编辑:
您可以使用DISTINCT
来获得不同的结果,但在您的表中有多行,因此将有多个结果,尽管您可以使用按风险分组参考
,但如果没有聚合函数,则不好用您可以尝试以下方法:
SELECT CONCAT_WS("/",
IF(working_people=1,"working_people",""),
IF(owner_occupied=1,"owner_occupied"),
IF(students=1,"students",""),
IF(dss_referrals=1,"dss_referrals",""),
IF(local_authority=1,"local_authority","")) AS result
FROM tbl_occupancy;
你可以试试:
SELECT CONCAT_WS("/",
IF(working_people=1,"working_people",""),
IF(owner_occupied=1,"owner_occupied"),
IF(students=1,"students",""),
IF(dss_referrals=1,"dss_referrals",""),
IF(local_authority=1,"local_authority","")) AS result
FROM tbl_occupancy;
你可以试试:
SELECT CONCAT_WS("/",
IF(working_people=1,"working_people",""),
IF(owner_occupied=1,"owner_occupied"),
IF(students=1,"students",""),
IF(dss_referrals=1,"dss_referrals",""),
IF(local_authority=1,"local_authority","")) AS result
FROM tbl_occupancy;
你可以试试:
SELECT CONCAT_WS("/",
IF(working_people=1,"working_people",""),
IF(owner_occupied=1,"owner_occupied"),
IF(students=1,"students",""),
IF(dss_referrals=1,"dss_referrals",""),
IF(local_authority=1,"local_authority","")) AS result
FROM tbl_occupancy;
你可以用
“concat ws”
。它不会跳过空字符串。但是,它会跳过分隔符和参数之后的任何空值
`mysql> SELECT CONCAT_WS(',','Test',NULL,'1');
output -> 'Test,1'`
你可以用
“concat ws”
。它不会跳过空字符串。但是,它会跳过分隔符和参数之后的任何空值
`mysql> SELECT CONCAT_WS(',','Test',NULL,'1');
output -> 'Test,1'`
你可以用
“concat ws”
。它不会跳过空字符串。但是,它会跳过分隔符和参数之后的任何空值
`mysql> SELECT CONCAT_WS(',','Test',NULL,'1');
output -> 'Test,1'`
你可以用
“concat ws”
。它不会跳过空字符串。但是,它会跳过分隔符和参数之后的任何空值
`mysql> SELECT CONCAT_WS(',','Test',NULL,'1');
output -> 'Test,1'`
我正在使用以下查询来获取我的解决方案:-
select group_concat("/",col.column_name) as hazards from information_schema.columns
as col inner join db_name.`tbl_occupancy` as oc on col.table_schema="db_name"
and col.table_name = "tbl_occupancy" and col.column_name in (
if(oc.working_people=1,"working_people",""),
if(oc.owner_occupied=1,"owner_occupied",""),
if(oc.students=1,"students",""),
if(oc.dss_referrals=1,"dss_referrals",""),
if(oc.local_authority=1,"local_authority","")
);
我正在使用以下查询来获取我的解决方案:-
select group_concat("/",col.column_name) as hazards from information_schema.columns
as col inner join db_name.`tbl_occupancy` as oc on col.table_schema="db_name"
and col.table_name = "tbl_occupancy" and col.column_name in (
if(oc.working_people=1,"working_people",""),
if(oc.owner_occupied=1,"owner_occupied",""),
if(oc.students=1,"students",""),
if(oc.dss_referrals=1,"dss_referrals",""),
if(oc.local_authority=1,"local_authority","")
);
我正在使用以下查询来获取我的解决方案:-
select group_concat("/",col.column_name) as hazards from information_schema.columns
as col inner join db_name.`tbl_occupancy` as oc on col.table_schema="db_name"
and col.table_name = "tbl_occupancy" and col.column_name in (
if(oc.working_people=1,"working_people",""),
if(oc.owner_occupied=1,"owner_occupied",""),
if(oc.students=1,"students",""),
if(oc.dss_referrals=1,"dss_referrals",""),
if(oc.local_authority=1,"local_authority","")
);
我正在使用以下查询来获取我的解决方案:-
select group_concat("/",col.column_name) as hazards from information_schema.columns
as col inner join db_name.`tbl_occupancy` as oc on col.table_schema="db_name"
and col.table_name = "tbl_occupancy" and col.column_name in (
if(oc.working_people=1,"working_people",""),
if(oc.owner_occupied=1,"owner_occupied",""),
if(oc.students=1,"students",""),
if(oc.dss_referrals=1,"dss_referrals",""),
if(oc.local_authority=1,"local_authority","")
);
或者您可以使用CONCAT_WS
这样就不会有尾随斜杠;)如果字段为空,则需要返回NULL
0@core1024是的,CONCAT_WS
可以作为替代品,但是Aviseks已经使用CONCAT_WS
提供了您的sql为我提供了多行输出。我需要一行。@MKhalidJunaid谢谢你的回答。它提供输出。我要放弃投票权。但我需要一些聪明的解决方案。所以我不能接受你的答案。或者你可以使用CONCAT\WS
这样就不会有尾随斜杠;)如果字段为空,则需要返回NULL
0@core1024是的,CONCAT_WS
可以作为替代品,但是Aviseks已经使用CONCAT_WS
提供了您的sql为我提供了多行输出。我需要一行。@MKhalidJunaid谢谢你的回答。它提供输出。我要放弃投票权。但我需要一些聪明的解决方案。所以我不能接受你的答案。或者你可以使用CONCAT\WS
这样就不会有尾随斜杠;)如果字段为空,则需要返回NULL
0@core1024是的,CONCAT_WS
可以作为替代品,但是Aviseks已经使用CONCAT_WS
提供了您的sql为我提供了多行输出。我需要一行。@MKhalidJunaid谢谢你的回答。它提供输出。我要放弃投票权。但我需要一些聪明的解决方案。所以我不能接受你的答案。或者你可以使用CONCAT\WS
这样就不会有尾随斜杠;)如果字段为空,则需要返回NULL
0@core1024是的,CONCAT_WS
可以作为替代品,但是Aviseks已经使用CONCAT_WS
提供了您的sql为我提供了多行输出。我需要一行。@MKhalidJunaid谢谢你的回答。它提供输出。我要放弃投票权。但我需要一些聪明的解决方案。所以我不能接受你的回答。请仔细阅读这个问题。我知道如何以及在哪里使用concat_ws()。请正确阅读问题。我知道如何以及在哪里使用concat_ws()。请正确阅读问题。我知道如何以及在哪里使用concat_ws()。请正确阅读问题。我知道如何以及在何处使用concat_ws()。如果其中一个/多个值不是'1'
,您希望如何输出?您是否也可以发布一个包含其他列及其名称的列表预期输出?@Ravinder检查我的编辑,如果可能,删除反对票。我需要这个问题的答案。我不是那个否决你的人。看来,这是你所期望的。你仍然期望从中得到什么样的改变?因为,除非你发布为什么它没有达到预期,否则我觉得,答案的所有者,除你之外的人,可能仍然在试图思考什么是错的!?,同样,我觉得这是不正确的。如果其中一个/多个值不是'1'
,您希望如何输出?可以