我有两张桌子:我正在进行加入,并希望返回一个查询,其中列出了每个GenEx处方品牌的多个代号。然而,看起来我正在进行连接的方式导致它超时。
药物:
ID | GenEx | CodeName | Desc
----------------------------
1 | Cipro | Dolvo |
2 | Ludavil | Ymir |
3 | Cipro | Alpha |
医学:
ID | GenEx | Price |
----------------------------
1 | Cipro | 4.99 |
2 | Ludavil | 12.99 |
3 | Benazol | 5.00 |
我想回来:
1. GenEx->Cipro, CodeName=>Dolvo,Alpha, Price->4.99
2. GenEx->Ludavil, CodeName=>Ymir, Price->12.99
SELECT GenEx, Price
GROUP_CONCAT(CodeName) as CodeName
FROM (`Drugs` d)
JOIN `Medicine` m ON `m`.`GenEx` = `d`.`GenEx`
WHERE GenEx
IN (
SELECT DISTINCT GenEx
FROM Drugs
WHERE codeName IN ('Alpha'))
)
GROUP BY `GenEx`;
答案 0 :(得分:1)
现在根据问题的最后更新更新答案。
试试这段代码:
SELECT d.`GenEx`, d.`CodeName`, d.`Price`,
GROUP_CONCAT(d.`CodeName`) as CodeName
FROM Drugs d
JOIN Medicine m
ON m.`GenEx` = d.`GenEx`
AND d.`GenEx`
IN (
SELECT DISTINCT `GenEx`
FROM drugs
WHERE codeName IN ('Alpha'))
)
GROUP BY d.`GenEx`;
让我知道你现在得到了什么。