如何显示子查询列

时间:2015-06-17 06:20:13

标签: mysql sql

我有这个简单的查询

SELECT * from switch_person where PTPK = (
SELECT PK from projects where TeamLead  = 1 and status = 1)

它正确显示了swith_person列。另外,我想显示projects表格列。 子查询表“projects”包括我要显示的列和switch_person列。这可能吗?

2 个答案:

答案 0 :(得分:2)

使用join代替子查询:

SELECT * 
from switch_person 
join project on PTPK = PK
where TeamLead  = 1 and status = 1

答案 1 :(得分:2)

为了选择多个列,连接到派生表而不是使用子查询,然后您可以选择多个派生表列:

SELECT x.PK, x.Col1, x.Col2, ...
from switch_person 
INNER JOIN
(
   SELECT PK, Col1, Col2
   FROM projects 
   WHERE TeamLead  = 1 and status = 1
) x
ON PTPK = x.PK;
相关问题