简体   繁体   中英

SQL Left Join Only One Row

By using this SQL Query:

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'

I get this result:

在此处输入图片说明

The Problem is, that I only want ONE row from each user. But when the user has more than one picture it is showing multiple rows.

What can I do, that I get only one row from each user?

Thanks

Add a GROUP BY clause

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM