Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL join 3 tables and format
    primarykey
    data
    text
    <p>I am using codeigniter and I am trying to call 3 different tables and join them together into one array. This is what I have:</p> <pre><code>$this-&gt;db-&gt;select('so_alcohol.id, so_alcohol.name as name, so_alcohol.category as category, so_alcohol.permName as permName, so_alcohol.picture as alcoholPicture, group_concat(so_user.firstName) as firstName, group_concat(so_user.lastName) as lastName, group_concat(so_user.picture) as userPicture, group_concat(so_rating.idUser) as idUser, group_concat(so_rating.overall) as overall, group_concat(so_rating.description) as description'); $this-&gt;db-&gt;from('alcohol'); $this-&gt;db-&gt;order_by("id", "desc"); $this-&gt;db-&gt;join('so_rating', 'so_rating.idAlcohol = so_alcohol.id', 'LEFT'); $this-&gt;db-&gt;join('so_user', 'so_user.id = so_rating.idUser', 'LEFT'); $query = $this-&gt;db-&gt;get(); </code></pre> <p>The output I would like is the following:</p> <pre><code>Array ( [0] =&gt; stdClass Object ( [id] =&gt; 1 [name] =&gt; Product Name [category] =&gt; Product category [permName] =&gt; Product-Name [alcoholPicture] =&gt; http://image.com/1.jpg [idUser] =&gt; 1,2,3 [overall] =&gt; 100,80,50 [description] =&gt; Review 1,Awesome review 2, Horrible review 3 [firstName] =&gt; name 1, name 2, name 3 [lastName] =&gt; last 1, last 2, last 3 [userPicture] =&gt; http://userimage.com/1.jpg, http://userimage.com/2.jpg, http://userimage.com/3.jpg ) [0] =&gt; stdClass Object ( [id] =&gt; 1 [name] =&gt; Product Name [category] =&gt; Product category [permName] =&gt; Product-Name [alcoholPicture] =&gt; http://image.com/1.jpg [idUser] =&gt; 1,2,3 [overall] =&gt; 100,80,50 [description] =&gt; Review 1,Awesome review 2, Horrible review 3 [firstName] =&gt; name 1, name 2, name 3 [lastName] =&gt; last 1, last 2, last 3 [userPicture] =&gt; http://userimage.com/1.jpg, http://userimage.com/2.jpg, http://userimage.com/3.jpg ) ) </code></pre> <p>My question is how do I make what I have work, right now there are two problems (that I have found) First it only outputs 1 result, I need it to output 40 and second I cant <code>so_user.id = so_rating.idUser</code> because I already <code>group_concat(so_rating.idUser)</code>Any Ideas? I am not even sure if this is the right way of doing it as I am still a php newbie. </p> <p>EDIT:</p> <p>Alcohol</p> <pre><code>id name category permName picture 1 Product #1 1 Product-1 http://someurl.com/1.jpg 2 Product #2 1 Product-2 http://someurl.com/2.jpg 3 Product #3 1 Product-2 http://someurl.com/3.jpg 4 Product #4 2 Product-2 http://someurl.com/4.jpg </code></pre> <p>Rating</p> <pre><code>id idUser idProduct overall description 1 1 2 100 Great Product Review 1 2 2 2 75 Great Product Review 2 3 1 3 100 Great Product Review 3 4 2 3 98 Great Product Review 4 </code></pre> <p>User </p> <pre><code>id firstName lastName userPicture 1 first 1 last 1 http://someurlUserPicture.com/1.jpg 2 first 2 last 2 http://someurlUserPicture.com/2.jpg 3 first 3 last 3 http://someurlUserPicture.com/3.jpg 4 first 4 last 4 http://someurlUserPicture.com/4.jpg </code></pre>
    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. 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