0

I was wondering how I could do this without using group_concat. Since my reviews are so long the group_concat is maxing out on them so it does not return all the reviews. The only other way I could think of doing it would be to loop the query. Just so you know there are several products and several reviews for each product. Any ideas?

    $this->db->select("product.id,
        product.name as name,
        group_concat(user.name) as user,
        group_concat(rating.overall) as overall,
        group_concat(rating.description SEPARATOR '|') as review");
    $this->db->join('rating', 'rating.idProduct = alcohol.id', 'LEFT');
    $this->db->join('user', 'user.id = rating.idUser', 'LEFT');
    $query = $this->db->get('product');
    return $query->result();

output as something like this:

[0] => stdClass Object (
        [name] => Product
        [reviews] => Array(
                [0]=> (
                        [user] => "cKendrick "
                        [overall] => "1"
                        [Rating] => "lalalalalala review"
                    )
                [1] = >
                    (...
    )

)
[1] => stdClass Object (..

UPDATE:

"SELECT product.id,
  product.name as name,
  category.permName as category,
  subCategory.permName as subCategory,
  product.permName as permName,
  product.picture as picture,
  user.username as username,
  user.firstName as firstName,
  user.lastName as lastName,
  user.picture as userPicture,
  rating.description as review
FROM (
     SELECT *
     FROM product
     LIMIT ?, 30
) product
LEFT JOIN category
    ON category.id = product.category
LEFT JOIN subCategory
    ON subCategory.id = product.subCategory
LEFT JOIN rating
    ON rating.idAlcohol = product.id
LEFT JOIN user
    ON user.id = rating.idUser
ORDER BY product.lastReview desc"

How would I restrict it by its category?

4

2 回答 2

0

Here are my assumptions:

You want to get all the products with associated ratings. You want to know who wrote which review.

 select product.id, product.name, rating.overall, rating.description, user.name 
from product 
left join rating on product.id = rating.productId 
left join on user.id=rating.userId;

If this is not what you want, edit your post and show a few rows of what you are looking for would look like.

于 2012-04-12T21:23:02.543 回答
0

Which DBAL are you using? In plain old PHP you could do it like this if you really want to use a single query -

<?php

$db = new PDO('dsn', 'user', 'pass');
$sql = "SELECT product.id, product.name, rating.overall, rating.description, user.name AS user  
        FROM (
           SELECT *
           FROM product
           LIMIT 30
        ) product   
        LEFT JOIN rating
            ON product.id = rating.productId   
        LEFT JOIN user
            ON user.id = rating.userId";

$stmt = $db->prepare($sql);
$stmt->execute();

$output = array();
while ($row = $stmt->fetchObject()) {
    $output[$row->id]['name'] = $row->name;
    $output[$row->id]['reviews'][] = array('user'        => $row->user,
                                           'overall'     => $row->overall,
                                           'description' => $row->description);
}

UPDATE - I have moved the product table into a subquery to enable limiting the number of products. Note that any filtering on the products should be added to the subquery not the outer select.

UPDATE - updated query based on update to question. As you want to filter products based on category before limiting the result the joins and criteria need to be added to the subquery -

SELECT product.id,
    product.name as name,
    product.category,
    product.subCategory,
    product.permName as permName,
    product.picture as picture,
    user.username as username,
    user.firstName as firstName,
    user.lastName as lastName,
    user.picture as userPicture,
    rating.description as review
FROM (
    SELECT
        p.*,
        category.permName AS category,
        subCategory.permName AS subCategory
    FROM product
    LEFT JOIN category
        ON category.id = product.category
    LEFT JOIN subCategory
        ON subCategory.id = product.subCategory
    WHERE category.permName = ?
    LIMIT ?, 30
) product
LEFT JOIN rating
    ON rating.idAlcohol = product.id
LEFT JOIN user
    ON user.id = rating.idUser
ORDER BY product.lastReview desc
于 2012-04-12T23:27:57.573 回答