我这是MySQL中的三个表。
艺术家姓名表(m) 带有产品iformations的表,在这种情况下图片地址(p) 具有艺术家ID和产品ID的表。 (PMX)
我正在尝试将他们加在一起,所以我为每个艺术家获得了一行。 1位艺术家的 p 表中有几张图片。我添加了一个名为mf_prior的列,其中是图像的数量(用户自己分配数字并选择他想要在前端显示的3个图像)。我正在寻找Letters的艺术家(制造商)。 “M”
我的想法是,我想得到
我正在
我有这个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.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'
因此我得到3行,并且在某些地方我得到NULL - 如上所示
也许任何人都可以帮我解决这个问题 - 编写一个更好的SELECT或用PHP结果制作smth
答案 0 :(得分:0)
所以我做到了。
新的SQL 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.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'
事情是改变前线:
加入
jos_vm_product AS p3 ON p1.product_id = pmx.product_id AND p3.mf_prior = 3
答案 1 :(得分:0)
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
这是对我的问题的正确和正确解决 - 我正在使用它并且工作正常,前一个问题不是100%正确,在某些情况下它正在崩溃。但这一个就是那个!!!