我正试图与自己联系,但没有运气。
我的表包含:
ID Name Position
1 Jefferson Roxas president
2 Carlson Zafiro president
3 Andrew Anderson vice president
9 Jayson Dela Cruz representative
10 Arille Villanueva Valdez representative
11 Arnold Baltazar representative
我想要的查询结果是:
ID Name Position ID Name Position ID Name Position
1 Jefferson Roxas president 3 Andrew Anderson vice president 9 Jayson Dela Cruz Representative
2 Carlson Zafiro president 10 Arille Villanueva Valdez Representative
11 Arnold Baltazar Representative
我正在使用此查询:
Select a.idcandidates, a.position, b.idcandidates, b.position , c.idcandidates, c.position
from tbl_candidates a, tbl_candidates b, tbl_candidates c
where a.position='president' and b.position='vice president' and c.position='representative'
但结果是
ID Name Position ID Name Position ID Name Position
1 Jefferson Roxas president 3 Andrew Anderson vice president 9 Jayson Dela Cruz Representative
2 Carlson Zafiro president 3 Andrew Anderson vice president 10 Arille Villanueva Valdez Representative
1 Jefferson Roxas president 3 Andrew Anderson vice president 11 Arnold Baltazar Representative
2 Carlson Zafiro president 3 Andrew Anderson vice president 9 Jayson Dela Cruz Representative
1 Jefferson Roxas president 3 Andrew Anderson vice president 10 Arille Villanueva Valdez Representative
2 Carlson Zafiro president 3 Andrew Anderson vice president 11 Arnold Baltazar Representative
答案 0 :(得分:0)
为什么不尝试使用三个单独的查询来执行UNION语句。例如。
Select Distinct a.idcandidates, a.position, b.idcandidates, b.position, c.idcandidates, c.position
from tbl_candidates a, tbl_candidates b, tbl_candidates c
where a.position='chairman'
UNION
Select Distinct a.idcandidates, a.position, b.idcandidates, b.position , c.idcandidates, c.position
from tbl_candidates a, tbl_candidates b, tbl_candidates c
where and b.position='vice chairman'
UNION
Select Distinct a.idcandidates, a.position, b.idcandidates, b.position , c.idcandidates, c.position
from tbl_candidates a, tbl_candidates b, tbl_candidates c
where c.position='representative'
我不知道这是否是您解决方案的替代方案。但是itws值得一试,看看你是否得到了你想要的结果。
答案 1 :(得分:0)
每行需要一个parentID来表示层次结构,然后您的查询将更容易编写和动态。如果您提供表格,我可以提供答案
答案 2 :(得分:0)
您想要的输出错过了关系查询的一些基本概念。我认为这几乎是其他答案在这里提到的。此外,您的查询与您提供的数据不一致。假设你真的是这个意思,
Select a.id, a.name, a.position, b.id, b.name, b.position , c.id, c.name, c.position
from tbl_candidates a, tbl_candidates b, tbl_candidates c
where a.position='president' and b.position='vice president' and c.position='representative'
然后,您所需的输出实际上只是这三个单独的查询恰好连接在它们的序数值上。如果是这种情况,那么让我们明确地这样做并将查询重新组合在一起。我不是MySQL的人,所以这就是我在SQL Server中的表现。
select
prs.id
,prs.name
,prs.position
,vcp.id
,vcp.name
,vcp.position
,rep.id
,rep.name
,rep.position
from (
select
id
,name
,position
,row_number() over (order by id) rwn
from
tbl_candidates
where
position = 'president'
) prs
full outer join (
select
id
,name
,position
,row_number() over (order by id) rwn
from
tbl_candidates
where
position = 'vice president'
) vcp on prs.rwn = vcp.rwn
full outer join (
select
id
,name
,position
,row_number() over (order by id) rwn
from
tbl_candidates
where
position = 'representative'
) rep on
rep.rwn = prs.rwn
or rep.rwn = vcp.rwn
不幸的是,mysql没有row_number()
。您可以尝试incorporate varables to build a row number instead。
这非常难看,所以我真的会考虑你的用例是什么,并试图改变它而不是这个解决方案。它确实提供了你想要的东西。