简体   繁体   English

如何使用两个不同的别名检索相同的列?

[英]how can i retrieve same column with two different alias?

I have one table called 'answers' with four column wherein adding answers with positive and negative rankings and i want to retrieve answer with different aliasing for answer with positive ranks and answer with negative ranks. 我有一个名为'答案'的表,其中有四列,其中添加了正面和负面排名的答案,我希望找到具有不同别名的答案,以便用正排名回答,并用负排名回答。 is there any way to retrieve same column with two different aliases ? 有没有办法检索具有两个不同别名的相同列?

id  answer  rank  question_id

1   Yes      1       1
2   No       2       1
3   True    -2       2
4   False   -1       2

I want to have this answers in the form of comma delimited list I have tried this but no success. 我想以逗号分隔列表的形式得到这个答案我试过这个但没有成功。

SELECT CASE WHEN a.rank > 0 THEN GROUP_CONCAT(a.answer)  END AS answer,
       CASE WHEN a.rank < 0 THEN GROUP_CONCAT(a.answer)  END AS matrix
FROM answers a.
select group_concat(case when rank > 0 then answer end) as pos_answers,
       group_concat(case when rank < 0 then answer end) as neg_answers
from answers

您可以尝试以下方法:

SELECT GROUP_CONCAT(IF(rank>0,answer,NULL)) as positive_ans, GROUP_CONCAT(IF(rank<0,answer,NULL)) as negative_ans FROM answers

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Mysql:如何检索具有相同 id 但列中值不同的两行 - Mysql: How can I retrieve two rows with same id but different values in column 如何根据MySQL中的两个不同条件从同一列中检索两个值? - How do I retrieve two values from the same column based on two different conditions in MySQL? 检索相同的数据存在于具有相同列结构的两个不同表中 - retrieve same data exists in two different tables with same column structure 如何在PHP和MySQL的同一列中添加两个不同的自动递增数字? - How can I add two different auto increment numbers to same column in PHP & MySQL? 选择具有不同别名和不同条件的同一列 - Select same column with different alias and different conditions 如何查询一个表的同一列中的两个不同对象,这些对象与另一个表中的不同列相关? - How can i make a query of two different objects in the same column of one table, relationed to different columns in another table? 在同一列上具有联合/联接和两个别名的MySQL - MySQL with union/join and two alias on same column 在 MariaDB/MySQL 中,如何从单行返回的不同记录中检索同一列? - In MariaDB/MySQL, how do I retrieve the same column from different records returned in a single row? 如何获得同一列中两个日期之间的天数? - How can I get days between two dates in the same column? 如何在两个或多个表中获取相同的列? - how can i fetch the same column in two or more tables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM