我有一个MySQL查询,试图获取所有包含数据的页面,如'word%'。我有一个名为Pages2Data的多对多表。看来要做到这一点,我需要有一个内部连接将Pages连接到Pages2Data表,然后是另一个将Pages2Data连接到Data的内部连接。
以下不起作用,因为嵌套的SELECT子句可以返回多行。我不知道如何解决它:
SELECT * FROM `Pages`
INNER JOIN `Pages2Data` ON
(`Pages2Data`.`DataID`=(SELECT `DataID` FROM `Data` WHERE `DataWord` LIKE 'word%'))
AND `Pages`.`PageID`=`Pages2Data`.`PageID`;
答案 0 :(得分:2)
SELECT * FROM `Pages`
INNER JOIN `Pages2Data`
ON `Pages`.`PageID`=`Pages2Data`.`PageID`
INNER JOIN `Data` ON `Data`.`DataID`= `Pages2Data`.`DataID`
WHERE `DataWord` LIKE 'word%';
答案 1 :(得分:1)
首先查询问题:
修正查询:
SELECT * FROM `Pages`
INNER JOIN `Pages2Data` ON `Pages`.`PageID`=`Pages2Data`.`PageID`
WHERE `Pages2Data`.`DataID`= (SELECT `DataID` FROM `Data` WHERE `DataWord` LIKE 'word%');
替代查询:
SELECT `PG`.*
FROM `Pages` `PG`
INNER JOIN `Pages2Data` `PD` ON `PD`.`PageID` = `PG`.`PageID`
INNER JOIN `Data` `DA` ON `PD`.`DataID` = `DA`.`DataID`
WHERE `DA`.`DataWord` LIKE 'word%';
答案 2 :(得分:0)
试试这个
SELECT * FROM `Pages` p, `Pages2Data` p2d, `Data` d
WHERE p.`PageID` = p2d.`PageID`
AND p2d.`DataID` = d.`DataID`
AND `DataWord` LIKE 'word%'
答案 3 :(得分:0)
你可以在where子句中查看'Like'。
SELECT * FROM
`Pages`
INNER JOIN
`Pages2Data` ON (`Pages`.`PageID`=`Pages2Data`.`PageID`)
WHERE
`DataWord` LIKE 'word%'
答案 4 :(得分:0)
SELECT *
FROM Pages
INNER JOIN Pages2Data
ON Pages.PageID = Pages2Data.PageID
INNER JOIN Data
ON Pages2Data.DataID = Data.DataID
WHERE DataWord LIKE 'word%'