SQL左连接只有一行

时间:2018-02-08 23:58:13

标签: mysql sql

使用此SQL查询:

SELECT pictures.IMAGELINK,userdata.BIRTHDATE,userdata.POSITION,InfiniteLove.USERNAME FROM 
InfiniteLove
LEFT JOIN pictures ON InfiniteLove.USERNAME = pictures.USERNAME
LEFT JOIN userdata ON InfiniteLove.USERNAME = userdata.USERNAME
WHERE NOT InfiniteLove.USERNAME = '".$USERNAME."' AND InfiniteLove.GENDER = 
'M' AND InfiniteLove.SEARCH = 'M' OR InfiniteLove.SEARCH = 'B'

我得到了这个结果:

enter image description here

问题是,我只想要每个用户一行。 但是当用户有多张图片时,它会显示多行。

我能做什么,每个用户只能获得一行?

由于

1 个答案:

答案 0 :(得分:-1)

添加GROUP BY子句

SELECT pictures.IMAGELINK,userdata.BIRTHDATE,userdata.POSITION,InfiniteLove.USERNAME FROM 
InfiniteLove
LEFT JOIN pictures ON InfiniteLove.USERNAME = pictures.USERNAME
LEFT JOIN userdata ON InfiniteLove.USERNAME = userdata.USERNAME
WHERE NOT InfiniteLove.USERNAME = '".$USERNAME."' AND InfiniteLove.GENDER = 
'M' AND InfiniteLove.SEARCH = 'M' OR InfiniteLove.SEARCH = 'B'
GROUP BY InfiniteLove.USERNAME