Jeg ved ikke, om der er en bedre løsning, men jeg tror, du kunne bruge denne:
SELECT p1.userID, p1.picture as pic1, p2.picture as pic2, p3.picture as pic3
FROM
pictures p1 left join pictures p2
on p1.userID=p2.userID and p1.picture<>p2.picture
left join pictures p3
on p1.userID=p3.userID and p1.picture<>p3.picture and p2.picture<>p3.picture
GROUP BY p1.userID
Dette vil vælge tre billeder for hver bruger. Hvis en bruger har mindre end tre billeder, vil den vise nuller, hvis den har flere, vælger den tre blandt dem alle.
Et alternativ, der viser tre billeder hver i en anden række, er denne forespørgsel, der gør brug af variabler:
SELECT userid, picture
FROM (
SELECT
userid,
picture,
case when @prec_id=userid then @row:[email protected]+1 else @row:=1 end as row,
@prec_id:=userid
FROM
`pictures`,
(SELECT @prec_id:=0, @row:=0) s
ORDER BY userid) s
WHERE row<=3
EDIT: for at vise tre billeder for hver bruger ad gangen ville jeg bruge min første forespørgsel, og jeg ville starte med noget kode som denne:
<?php
$mysqli = new mysqli("localhost", "username", "password", "test");
$image_path = "../images/";
$no_image = "../image/no_image.jpg";
if(!isset($_GET['first'])){
$first = 0;
} else {
$first = (int) $_GET['first'];
}
if ($stmt = $mysqli->prepare("SELECT p1.userID, p1.picture as pic1, p2.picture as pic2, p3.picture as pic3
FROM
pictures p1 left join pictures p2
on p1.userID=p2.userID and p1.picture<>p2.picture
left join pictures p3
on p1.userID=p3.userID and p1.picture<>p3.picture and p2.picture<>p3.picture
GROUP BY p1.userID
LIMIT ?,1")) {
$stmt->bind_param("i", $first);
$stmt->execute();
$stmt->bind_result($user, $pic1, $pic2, $pic3);
$stmt->fetch();
$stmt->close();
}
$mysqli->close();
?>
<div style="position:absolute; top:50px; left:100px; width:800px; text-align: center;">
<img src="<?PHP echo (isset($pic1) ? $image_path.$pic1 : $no_image); ?>" width="176px" height="197px">
<img src="<?PHP echo (isset($pic2) ? $image_path.$pic2 : $no_image); ?>" width="176px" height="197px">
<img src="<?PHP echo (isset($pic3) ? $image_path.$pic3 : $no_image); ?>" width="176px" height="197px">
</div>
(det er blevet forbedret, men du kan starte med det. Jeg bruger mysqli i stedet for mysql)