简体   繁体   English

PHP INNER JOIN sql查询具有多个表的重复信息

[英]PHP INNER JOIN sql query with multiple tables duplicated information

I'm having some trouble to make a query that get all the information from multiple tables with INNER JOIN. 我在使用INNER JOIN进行查询以从多个表中获取所有信息时遇到了一些麻烦。 The table p_cards is the "main table" that have a relationship 1-M. p_cards是具有1-M关系的“主表”。 This means that every other table ( p_images , p_infotext , p_rules can have multiple rows associated with the same ID) 这意味着每隔一个表( p_imagesp_infotextp_rules可以具有与同一ID关联的多个行)

My function is the following: 我的功能如下:

function GetpById($mysqli, $id) {
    if($stmt = $mysqli->prepare("
        SELECT * 
        FROM p_cards 
        INNER JOIN p_images ON p_images.card_id = p_cards.id
        INNER JOIN p_infotext ON p_infotext.card_id = p_cards.id
        INNER JOIN p_rules ON p_rules.card_id = p_cards.id
        WHERE p_cards.id = ?
    ")) {
        $stmt->bind_param("i", $id);
        $stmt->execute() or trigger_error($stmt->error, E_USER_ERROR);
        ($stmt_result = $stmt->get_result()) or trigger_error($stmt->error, E_USER_ERROR);
        if ($stmt_result->num_rows>0) {
            while($row_data = $stmt_result->fetch_assoc()){
                $array[] = $row_data;
            }
            return $array   
        } else {
            return false;
        }
    } else {
        return false;
    }
}

Using the above function it will create a complete row for each different row inside the other tables, making the information in p_cards duplicated for each. 使用上述功能,它将为其他表中的每个不同行创建一个完整行,从而使p_cards中的信息重复。 For example, the p_images table will have id , p_id , image , language . 例如,p_images表将具有idp_idimagelanguage For each image that exists associated with that ID, it will associate the information of p_cards. 对于每个存在的与该ID关联的图像,它将关联p_cards的信息。

What I would like is not to have duplicated information. 我想要的是没有重复的信息。 How can I achieve this? 我该如何实现?

Edit: 编辑:

Table p_cards 表p_cards 表p_cards

Table p_images 表p_images t p_images

There are other tables but it's enough to show it (since there are many tables, every of them working as 1-M) 还有其他表,但足以显示它(因为有很多表,每个表都以1-M工作)

When I do the query, I get the information of p_card for each information in p_images (and other tables): 当我执行查询时,对于p_images(和其他表)中的每个信息,我都会得到p_card的信息: 在此处输入图片说明

This way I expect to get something like this so I can use the information to make a form to edit the data: 通过这种方式,我希望得到类似的信息,因此我可以使用该信息来制作表格来编辑数据:

id
slug
card_number
...
--[images]
---[en]
----thumb_img
----full_size
----name
---[pt]
----thumb_img
...

Instead of having one row with all information. 而不是将所有信息都排成一行。 What would be the best approach? 最好的方法是什么?

You get multiple rows for the same card id because you have multiple rows for it in the tables you join with. 对于同一个卡号,您会得到多行,因为在与之连接的表中有多行它。

If you want to have only one row per card id, you can use GROUP BY p_cards.id, and then you can get a comma separated list of the different values in the other tables using GROUP_CONCAT() function. 如果每个卡ID仅希望有一行,则可以使用GROUP BY p_cards.id,然后使用GROUP_CONCAT()函数获得其他表中不同值的逗号分隔列表。

However, in your situation I think it is better to do 3 seprate queries instead of doing this in a join. 但是,在您的情况下,我认为最好执行3个单独的查询,而不要在联接中执行此操作。

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

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