我的查询有问题。首先这是我的数据库结构

database mt
- mtnobuk
- mtbrg
- mtcolor
- mtloc

database mv
- mvnobuk
- mvrng
- mvmsn
- mtnobuk (FK)


这是我的数据

MT
mtnobuk     mtbrg   mtcolor     mtloc
---------------------------------------------
MT-112      Shape    Blue       L1

MV
mvnobuk     mvrng   mvmsn       mtnobuk
-------------------------------------------------
MV-111      round   sharp       MT-112
MV-112      oval    blunt       MT-112


我想要的就是我要查询这样的数据-

mtnobuk     mtbrg       mtcolor       mvrng         mvmsn
----------------------------------------------------------
MT-112      Shape        Blue         round         sharp
                                      oval          blunt
----------------------------------------------------------


到目前为止,我已经尝试过了

SELECT  mtnobuk,mtbrg,mtcolor,mtloc,mvnobuk,mvrng,mvmsn,mtnobuk
FROM    mv
LEFT JOIN mt on mtnobuk = mvnobuk
WHERE mtnobuk = MT-112
GROUP BY mtnobuk,mtbrg


我已经尝试了很多次,而我得到的只是双重结果,像这样

mtnobuk     mtbrg       mtcolor       mvrng         mvmsn
----------------------------------------------------------
MT-112      Shape        Blue         round         sharp
                                      oval          blunt
----------------------------------------------------------
MT-112      Shape        Blue         round         sharp
                                      oval          blunt
----------------------------------------------------------


我希望有人能帮助我解决这个问题,谢谢大家,并祝您愉快。

最佳答案

我认为GROUP CONCAT可能是您最好的选择。像这样尝试:

SELECT  mtnobuk, mtbrg, mtcolor, mvnobuk, GROUP_CONCAT(mvrng), GROUP_CONCAT(mvmsn)
FROM    mv
LEFT JOIN mt on mtnobuk = mvnobuk
WHERE mtnobuk = MT-112
GROUP BY mtnobuk, mtbrg, mtcolor, mvnobuk

10-08 05:19