从3个不同的表中查询几行

时间:2012-04-12 20:39:52

标签: php mysql

我想知道如何在不使用group_concat的情况下做到这一点。由于我的评论太长,所以group_concat最大限度地使用它们,因此它不会返回所有评论。我能想到的另一种方法是循环查询。您知道每种产品都有几种产品和几种评论。有什么想法吗?

    $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();

输出如下:

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

)
[1] => stdClass Object (..

更新:

"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"

我如何按类别限制它?

2 个答案:

答案 0 :(得分:0)

以下是我的假设:

您希望获得具有相关评级的所有产品。你想知道谁写了哪篇评论。

 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;

如果这不是您想要的,请编辑您的帖子并显示您要查找的内容的几行。

答案 1 :(得分:0)

您使用的是哪个DBAL?在普通的旧PHP中,如果你真的想使用单个查询,你就可以这样做 -

<?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);
}

更新 - 我已将产品表移动到子查询中以启用限制产品数量。请注意,对产品的任何过滤都应添加到子查询中,而不是外部选择。

更新 - 根据问题更新更新的查询。由于您希望在限制结果之前根据类别过滤产品,因此需要将连接和条件添加到子查询中 -

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