简体   繁体   English

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

[英]Query several rows from 3 different tables

I was wondering how I could do this without using group_concat. 我想知道如何在不使用group_concat的情况下做到这一点。 Since my reviews are so long the group_concat is maxing out on them so it does not return all the reviews. 由于我的评论太长了,group_concat正在使他们的评论最大化,因此它不会返回所有评论。 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? 我如何按类别限制它?

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. 如果这不是您想要的,请编辑您的帖子,并显示几行您想要的内容。

Which DBAL are you using? 您正在使用哪个DBAL? In plain old PHP you could do it like this if you really want to use a single query - 如果您确实想使用单个查询,则在普通的旧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);
}

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. UPDATE-根据问题的更新来更新查询。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM