Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't know if there's a better solution, but i think you could use this:</p> <pre><code>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&lt;&gt;p2.picture left join pictures p3 on p1.userID=p3.userID and p1.picture&lt;&gt;p3.picture and p2.picture&lt;&gt;p3.picture GROUP BY p1.userID </code></pre> <p>This will select three images for each user. If a user has less than three images, it will show nulls, if it has more, it chooses three between all of them.</p> <p>An alternative, that show three images each one in a different row, is this query that makes use of variables:</p> <pre><code>SELECT userid, picture FROM ( SELECT userid, picture, case when @prec_id=userid then @row:=@row+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&lt;=3 </code></pre> <p><strong>EDIT:</strong> to show three images for each user at a time I would use my first query, and I would start with some code like this:</p> <pre><code>&lt;?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-&gt;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&lt;&gt;p2.picture left join pictures p3 on p1.userID=p3.userID and p1.picture&lt;&gt;p3.picture and p2.picture&lt;&gt;p3.picture GROUP BY p1.userID LIMIT ?,1")) { $stmt-&gt;bind_param("i", $first); $stmt-&gt;execute(); $stmt-&gt;bind_result($user, $pic1, $pic2, $pic3); $stmt-&gt;fetch(); $stmt-&gt;close(); } $mysqli-&gt;close(); ?&gt; &lt;div style="position:absolute; top:50px; left:100px; width:800px; text-align: center;"&gt; &lt;img src="&lt;?PHP echo (isset($pic1) ? $image_path.$pic1 : $no_image); ?&gt;" width="176px" height="197px"&gt; &lt;img src="&lt;?PHP echo (isset($pic2) ? $image_path.$pic2 : $no_image); ?&gt;" width="176px" height="197px"&gt; &lt;img src="&lt;?PHP echo (isset($pic3) ? $image_path.$pic3 : $no_image); ?&gt;" width="176px" height="197px"&gt; &lt;/div&gt; </code></pre> <p>(it has be improved, but you could start with it. I'm using mysqli instead of mysql)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload