我正在尝试为每个产品显示一行,但每个产品都有多个值。 这就是我正在尝试的:
select pr.ID_PRO, pr.NOMBRE_PRODUCTO, img.DIRECCION, m.MARCA_NOMBRE from
productos pr INNER JOIN
img_pro img ON pr.ID_PRO = img.FOR_PRO INNER JOIN
mm_productos_marca pm ON pm.ID_PRO = pr.ID_PRO INNER JOIN
marca m ON m.ID_MAR = pm.ID_MAR;
结果:
+--------+-----------------+---------------------------+--------------+
| ID_PRO | NOMBRE_PRODUCTO | DIRECCION | MARCA_NOMBRE |
+--------+-----------------+---------------------------+--------------+
| 1 | asdasd | images/discos-esmeril.jpg | ABRATOOLS |
| 1 | asdasd | images/lol.png | ABRATOOLS |
| 2 | 123qwe1 | images/images.jpg | ABRATOOLS |
| 2 | 123qwe1 | images/images.jpg | BRAND |
+--------+-----------------+---------------------------+--------------+
欲望结果:
+--------+-----------------+---------------------------+--------------+
| ID_PRO | NOMBRE_PRODUCTO | DIRECCION | MARCA_NOMBRE |
+--------+-----------------+---------------------------+--------------+
| 1 | asdasd | images/discos-esmeril.jpg | ABRATOOLS |
| | | images/lol.png | |
+--------+-----------------+---------------------------+--------------+
| 2 | 123qwe1 | images/images.jpg | ABRATOOLS |
| | | | BRAND |
+--------+-----------------+---------------------------+--------------+
答案 0 :(得分:0)
如果您对逗号分隔DIRECCION
值感到满意(在我看来这是最佳解决方案),那么您可以使用以下查询
select ID_PRO,NOMBRE_PRODUCTO,group_concat(DIRECCION)as
DIRECCION,MARCA_NOMBRE from tab1 group by NOMBRE_PRODUCTO
order by id_pro
逗号分隔值使处理更容易