我想要显示未显示的行,但获取错误子查询返回的行数超过1行
这很好用;
SELECT CONCAT( P.LastName, ', ', P.FirstName ) AS Name,
(
SELECT MP.MembershipID
FROM membershipperson MP
WHERE MP.PersonID = P.PersonID
)
AS MembershipID, PersonID
FROM `person` P
当我添加ORDER BY'MembershipID'时,如果我按ORDER BY'PersonID',它就会失败;
SELECT CONCAT( P.LastName, ', ', P.FirstName ) AS Name,
(
SELECT MP.MembershipID
FROM membershipperson MP
WHERE MP.PersonID = P.PersonID
)
AS MembershipID, PersonID
FROM `person` P
ORDER BY `MembershipID` ASC
如何获取包含所有MembershipID行的列表?
答案 0 :(得分:1)
您应该使用内部联接。像这样:
SELECT CONCAT( P.LastName, ', ', P.FirstName ) AS Name, MP.MembershipID AS MembershipID, PersonID
FROM person P, membershipperson MP
WHERE MP.PersonID = P.PersonID
答案 1 :(得分:0)
使用子查询是否重要?您可以使用INNER JOIN
编写它:
SELECT
CONCAT( P.LastName, ', ', P.FirstName ) AS Name,
MP.MembershipID,
P.PersonID
FROM `person` P
INNER JOIN `membershipperson` MP ON MP.PersonID = P.PersonID
ORDER BY MP.MembershipID ASC