[英]Group_concat with columns of a particular type in the first
My tables are: 我的表是:
Parent_Child (Parent_SSN, Child_SSN)
Person (SSN, Name, age, sex)
School (Child_SSN, School_Name)
I want to select the parents(female,male) who have atleast one of their children in a particular school 'X'
.I have a working query and my mysql query is: 我想选择在特定学校
'X'
至少有一个孩子的父母(女,男)。我有一个有效的查询,我的mysql查询是:
select group_concat(p.name) from person p,parentchild pc,school s
where s.schoolname='X' and s.childssn=pc.childssn and p.ssn=pc.parentssn
group by pc.childssn
This displays the result as parent(male,female)
but I want the result in (female,male) form
and if I group it by parent.sex it displays results in individual rows and not in a single row.I am out of ideas. 这会将结果显示为
parent(male,female)
但我想要以(female,male) form
显示结果,如果按parent.sex将其分组,它将结果显示在单独的行中,而不是在单行中显示。 。
Sample desired output: 所需输出样本:
name
Angela,Jim
Output of my above existing query: 我上面的现有查询的输出:
name
Jim,Angela
Here's a Sql Fiddle for you. 这是给您的Sql Fiddle 。
SELECT DISTINCT group_concat(p.name ORDER BY p.sex Asc)
FROM Person p JOIN Parent_Child pc ON p.ssn=pc.Parent_SSN
JOIN School s ON s.Child_SSN = pc.Child_SSN
WHERE s.School_Name='X'
GROUP By pc.Child_SSN;
Try this, might possible using sort 试试这个,可能可以使用sort
SELECT GROUP_CONCAT( p.name ORDER BY p.name DESC ) from person p,parentchild pc,school s
where s.schoolname='X' and s.childssn=pc.childssn and p.ssn=pc.parentssn
group by pc.childssn
You can use ORDER BY inside the GROUP_CONCAT function in this way : 您可以通过以下方式在GROUP_CONCAT函数中使用ORDER BY:
Try this 尝试这个
SELECT group_concat(DISTINCT p.name ORDER BY p.name Asc)
FROM person p JOIN parentchild pc ON p.ssn=pc.parentssn
JOIN school s ON s.childssn = pc.childssn
WHERE s.schoolname='X'
GROUP By pc.childssn
See http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function%5Fgroup-concat 参见http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function%5Fgroup-concat
I somehow managed to get the results using join here is my working query. 我以某种方式设法通过join获得了结果,这是我的工作查询。
select * from (select distinct p.name from person p,parentchild pc,school s where s.schoolname='X' and s.childssn=pc.childssn and p.ssn=pc.parentssn and p.sex='F')q1 join (select distinct p.name from person p,parentchild pc,school s where s.schoolname='X' and s.childssn=pc.childssn and p.ssn=pc.parentssn and p.sex='M')q
I want to still know whether it is possible to sort items by a particular condition in group_concat result set which will affect it's postion in the group_concat result set. 我仍然想知道是否可以按group_concat结果集中的特定条件对项目进行排序,这将影响它在group_concat结果集中的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.