简体   繁体   中英

php mysql join results set repeating data

I've read almost every other post that relates to my query and cant find a definitive answer - I apologise if this has already been answered.

Basically I have a products table and an images table, 1 product can have many images. It is my understanding that it will be better performance wise to get all my data with a single query than run multiple queries.

I am currently using a left join query which works perfectly well but to me seems inefficient.

My results set has lots of repeating data (I know that's what I'm asking for) and is due to the 'products' table contains 50 (ish) columns where as the 'images' table only has 3 columns so my resultset looks like this:

P_ID    P_NAME    P_DESC   -->(rest of product data)    I_ID    I_PATH    I_THUMB
001     a.prod    'This is a product'                   001-01  /a1.jpg   /a1t.jpg
001     a.prod    'This is a product'                   001-02  /a2.jpg   /a2t.jpg
001     a.prod    'This is a product'                   001-03  /a3.jpg   /a3t.jpg
001     a.prod    'This is a product'                   001-04  /a4.jpg   /a4t.jpg

I_ID is the primary key in the images table and is a column in the 'products' table which is where the relationship between the tables exists.

Some products can have 10 or 15 images which will replicate the 'product' information 10 or 15 times, to me this just seems wrong. Having so much repeating data when it is not necessary.

I suppose what I'm really asking is: is this the best approach or should I have 2 queries 1 to find the product based on it's ID then another to find the images that relate to the product from query 1.

I was expecting a 1 to many relationship to return something like this:

P_ID    P_NAME    P_DESC   -->(rest of product data)    I_ID    I_PATH    I_THUMB
001     a.prod    'This is a product'                   001-01  /a1.jpg   /a1t.jpg
NULL    NULL      NULL                                  001-02  /a2.jpg   /a2t.jpg
NULL    NULL      NULL                                  001-03  /a3.jpg   /a3t.jpg
NULL    NULL      NULL                                  001-04  /a4.jpg   /a4t.jpg

I was also thinking it would be best to get a multidimensional array back which looks something like:

$resultset = array(
    'P_ID'=>'001', 
    'P_NAME'=>'a.prod',
    'P_DESC'=>'This is a product'
    (rest of product data) 
    'IMAGES'=>array(
        [0] => Array(
            'I_ID'=>'001-01',
            'I_PATH'=>'/a1.jpg', 
            'I_THUMB'=>'/a1t.jpg')
        [1] => Array(
             'I_ID'=>'001-02',
             'I_PATH'=>'/a2.jpg', 
             'I_THUMB'=>'/a2t.jpg')
        [2] => Array(
             'I_ID'=>'001-03',
             'I_PATH'=>'/a3.jpg', 
             'I_THUMB'=>'/a3t.jpg')
        [3] => Array(
             'I_ID'=>'001-04',
             'I_PATH'=>'/a4.jpg', 
             'I_THUMB'=>'/a4t.jpg')
    )
);

But I cant for the life of me work out how to get the data back like this - please HELP

It sounds like you should use 2 separate queries. One to return the product info and one to return the images. Otherwise your queries will always repeat the information from the product table for each image it matches to. Performance shouldn't be an issue as long as the P_ID field is indexed in each table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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