开发者

Align information from MySQL in 1 row

开发者 https://www.devze.com 2023-02-04 00:31 出处:网络
I this three tables in MySQL. Table with names of Artists (m) Table with Product iformations, in this case Picture addresses (p)

I this three tables in MySQL.

Table with names of Artists (m) Table with Product iformations, in this case Picture addresses (p) Table that has id of Artists and id of Products. (pmx)

I'm trying to Join them together so I get 1 row for each Artists. There are several images in p table for 1 Artists. I added a column called mf_prior where are the numbers of images (user assigns the numbers hiself and select those 3 images that he wants to be displayd on the front-end). I'm searching Artists (manufacturers) by Letters for ex. "M"

The idea is that I'd like to get

  1. manufacturer_id | mf_name | img1_id | img1_address | img2_id | img2_address | img3_id | img3_address

I'm getting

  1. manufacturer_id1 | mf_name1 | img1_id | img1_address | NULL | NULL | NULL | NULL
  2. manufacturer_id1 | mf_name1 | NULL | NULL | img2_id | img2_address | NULL | NULL
  3. manufacturer_id1 | mf_name1 | NULL | MULL | NULL | NULL | img3_id | img3_address

I have this MySQL SELECT:

SELECT
 m.manufacturer_id ,
 m.mf_name ,
 p1.product_id AS pimg1 ,
 p1.product_full_image AS p1 ,
 p2.product_id AS pimg2 ,
 p2.product_full_image AS p2 ,
 p3.product_id AS pimg3 ,
 p3.开发者_JAVA百科product_full_image AS p3 
FROM
 jos_vm_product_mf_xref AS pmx 
JOIN
 jos_vm_manufacturer AS m ON m.manufacturer_id = pmx.manufacturer_id 
JOIN
 jos_vm_product AS p1 ON p1.product_id = pmx.product_id AND p1.mf_prior = 1
JOIN
 jos_vm_product AS p2 ON p2.product_id = pmx.product_id AND p2.mf_prior = 2
JOIN
 jos_vm_product AS p3 ON p3.product_id = pmx.product_id AND p3.mf_prior = 3

WHERE
 m.mf_chars = 'm'

as a result I get 3 rows and in places I get NULL - as shown above

Maybe anyone can help me with this one - to write a better SELECT or make smth with the result with PHP


So I made it.

The new SQL SELECT looks like this:

SELECT
    m.manufacturer_id ,
    m.mf_name ,
    p1.product_id AS pimg1 ,
    p1.product_full_image AS p1 ,
    p2.product_id AS pimg2 ,
    p2.product_full_image AS p2 ,
    p3.product_id AS pimg3 ,
    p3.product_full_image AS p3 
FROM
    jos_vm_product_mf_xref AS pmx 
JOIN
    jos_vm_manufacturer AS m ON m.manufacturer_id = pmx.manufacturer_id 
JOIN
    jos_vm_product AS p1 ON p1.product_id = pmx.product_id AND p1.mf_prior = 1
JOIN
    jos_vm_product AS p2 ON p1.product_id = pmx.product_id AND p2.mf_prior = 2
JOIN
    jos_vm_product AS p3 ON p1.product_id = pmx.product_id AND p3.mf_prior = 3
WHERE
    m.mf_chars = 'm'

the thing is change on lines ex.:

JOIN

jos_vm_product AS p3 ON p1.product_id = pmx.product_id AND p3.mf_prior = 3


SELECT 
    ts.manufacturer_id ,
    tb1.mf_name ,
    ts.S1ID AS V1ID,
    tb21.product_full_image AS V1 ,
    ts.S2ID AS V2ID,
    tb22.product_full_image AS V2 ,
    ts.S3ID AS V3ID,
    tb23.product_full_image AS V3 
FROM
    (
        SELECT 
            tbs.manufacturer_id AS manufacturer_id,
            MAX(S1) AS sn1,
            MAX(S1ID) AS S1ID ,
            MAX(S2) AS sn2,
            MAX(S2ID) AS S2ID ,
            MAX(S3) AS sn3 ,
            MAX(S3ID) AS S3ID 
        FROM 
            (
                SELECT 
                    vs.manufacturer_id ,
                    CASE
                        WHEN t2.mf_prior = 1 THEN 1 ELSE 0
                    END AS S1 ,
                    CASE
                        WHEN t2.mf_prior = 1 THEN t2.product_id ELSE 0
                    END AS S1ID ,
                    CASE
                        WHEN t2.mf_prior = 2 THEN 2 ELSE 0
                    END AS S2 ,
                    CASE
                        WHEN t2.mf_prior = 2 THEN t2.product_id ELSE 0
                    END AS S2ID ,
                    CASE
                        WHEN t2.mf_prior = 3 THEN 3 ELSE 0
                    END AS S3 ,
                    CASE
                        WHEN t2.mf_prior = 3 THEN t2.product_id ELSE 0
                    END AS S3ID 
                FROM
                    jos_vm_product AS t2
                JOIN
                    jos_vm_product_mf_xref AS vs ON vs.product_id = t2.product_id
            ) tbs
        GROUP BY
            tbs.manufacturer_id
    ) ts
JOIN
    jos_vm_manufacturer AS tb1 ON ts.manufacturer_id = tb1.manufacturer_id 
LEFT JOIN
    jos_vm_product AS tb21 ON ts.S1ID = tb21.product_id AND ts.sn1 = tb21.mf_prior 
LEFT JOIN
    jos_vm_product AS tb22 ON ts.S2ID = tb22.product_id AND ts.sn2 = tb22.mf_prior 
LEFT JOIN
    jos_vm_product AS tb23 ON ts.S3ID = tb23.product_id AND ts.sn3 = tb23.mf_prior

this is the right and correct solving to my question - I'm using it and it works fine, the previous question isn't 100% corret, in some cases it was breaking down. But this one is the very one!!!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号