sql 加入还是子查询?

sql join or subquery?

首先感谢您阅读本文。

我想在 2 table 秒之间进行查询,但我不知道该怎么做。

我有一个 table 叫 products 和另一个叫 product_photos。我想查询所有 products,并在结果的每个通道中,添加来自 table product_photos 的两个字段。问题是当我执行我的查询工作但只显示 product_photos 的第一个字段时,我想显示每个车道。

我知道了:

select p.*, ps.url_little, ps.url_big
from product p
     LEFT join product_photos ps
         on (p.id_prod = ps.id_product)

我该怎么做?我必须做子查询或联合吗?谢谢大家

编辑:

json 结果示例:

{\"id\":\"1\",\"id_prod\":\"375843\",\"ref\":\"5943853\",\"ean\":\"894378432831283\",\"concept\":\"Portamatr\u00edculas Barracuda\",\"description\":\"Portamatr\u00edculas Barracuda FZ6 a\u00f1o 2004-2008\",\"price\":\"19.99\",\"old_price\":\"25.58\",\"category\":\"Motor\",\"family\":\"Accesorio veh\u00edculo a motor\",\"sub_family\":\"Accesorio veh\u00edculo a motor de dos ruedas\",\"gender\":\"\",\"sub_gender\":\"\",\"photo\":\"\",\"thumbnail\":\"\",\"type\":\"1\",\"size\":\"\",\"color\":\"\",\"weave\":\"\",\"motiu\":\"\",\"material\":\"\",\"artist\":\"\",\"technique\":\"\",\"paper\":\"\",\"tittle\":\"\",\"measure\":\"\",\"edition\":\"\",\"status\":\"\",\"reference\":\"\",\"cost\":\"0\",\"url_little\":\"urllittlee kgjhdfjfd\",\"url_big\":\"url bigota\"}

如您所见,我得到了两个字段,url_little 和 url_big,但仅来自 1 个字段,我在 table product_photos 中得到了两个字段.我希望两者都出现。

第二次编辑,我很难解释我的问题,抱歉:

我收到这个 json:

{\"id\":\"1\",\"id_prod\":\"375843\",\"ref\":\"5943853\",\"ean\":\"894378432831283\",\"concept\":\"Portamatr\u00edculas Barracuda\",\"description\":\"Portamatr\u00edculas Barracuda FZ6 a\u00f1o 2004-2008\",\"price\":\"19.99\",\"old_price\":\"25.58\",\"category\":\"Motor\",\"family\":\"Accesorio veh\u00edculo a motor\",\"sub_family\":\"Accesorio veh\u00edculo a motor de dos ruedas\",\"gender\":\"\",\"sub_gender\":\"\",\"photo\":\"\",\"thumbnail\":\"\",\"type\":\"1\",\"size\":\"\",\"color\":\"\",\"weave\":\"\",\"motiu\":\"\",\"material\":\"\",\"artist\":\"\",\"technique\":\"\",\"paper\":\"\",\"tittle\":\"\",\"measure\":\"\",\"edition\":\"\",\"status\":\"\",\"reference\":\"\",\"cost\":\"0\",\"url_little\":\"urllittlee kgjhdfjfd\",\"url_big\":\"url bigota\"},{\"id\":\"1\",\"id_prod\":\"375843\",\"ref\":\"5943853\",\"ean\":\"894378432831283\",\"concept\":\"Portamatr\u00edculas Barracuda\",\"description\":\"Portamatr\u00edculas Barracuda FZ6 a\u00f1o 2004-2008\",\"price\":\"19.99\",\"old_price\":\"25.58\",\"category\":\"Motor\",\"family\":\"Accesorio veh\u00edculo a motor\",\"sub_family\":\"Accesorio veh\u00edculo a motor de dos ruedas\",\"gender\":\"\",\"sub_gender\":\"\",\"photo\":\"\",\"thumbnail\":\"\",\"type\":\"1\",\"size\":\"\",\"color\":\"\",\"weave\":\"\",\"motiu\":\"\",\"material\":\"\",\"artist\":\"\",\"technique\":\"\",\"paper\":\"\",\"tittle\":\"\",\"measure\":\"\",\"edition\":\"\",\"status\":\"\",\"reference\":\"\",\"cost\":\"0\",\"url_little\":\"SISI\",\"url_big\":\"NONO\"}

我想收到这个:

{\"id\":\"1\",\"id_prod\":\"375843\",\"ref\":\"5943853\",\"ean\":\"894378432831283\",\"concept\":\"Portamatr\u00edculas Barracuda\",\"description\":\"Portamatr\u00edculas Barracuda FZ6 a\u00f1o 2004-2008\",\"price\":\"19.99\",\"old_price\":\"25.58\",\"category\":\"Motor\",\"family\":\"Accesorio veh\u00edculo a motor\",\"sub_family\":\"Accesorio veh\u00edculo a motor de dos ruedas\",\"gender\":\"\",\"sub_gender\":\"\",\"photo\":\"\",\"thumbnail\":\"\",\"type\":\"1\",\"size\":\"\",\"color\":\"\",\"weave\":\"\",\"motiu\":\"\",\"material\":\"\",\"artist\":\"\",\"technique\":\"\",\"paper\":\"\",\"tittle\":\"\",\"measure\":\"\",\"edition\":\"\",\"status\":\"\",\"reference\":\"\",\"cost\":\"0\",\"url_little\":\"urllittlee kgjhdfjfd , SISI\",\"url_big\":\"url bigota, NONO\"}

如您所见,url_little 和 url_big 字段中是所有结果,而不仅仅是第一个。

谢谢!

您可以使用 FULL OUTER JOIN 关键字。 在你的情况下,这意味着这样的事情:

SELECT p.*, ps.url_little, ps.url_big from 
FROM product p
FULL OUTER JOIN product_photos ps
ON (p.id_prod = ps.id_product);

这将为您留下产品的完整照片。

从最后的编辑来看,我想这就是你想要的。它使用 GROUP_CONCAT 将链接的 table 中的所有值连接在一起。

select p.*,
group_concat(ps.url_little SEPARATOR ', '),
group_concat(ps.url_big SEPARATOR ', ')
from product p
     LEFT join product_photos ps
         on (p.id_prod = ps.id_product)
group by p.id_prod

在Mysql中你可以使用GROUP_CONCAT

SELECT p.*, ps.urls_little, ps.urls_big
FROM product p
    LEFT join
(SELECT id_product, GROUP_CONCAT(url_little) AS urls_little, GROUP_CONCAT(url_big) AS urls_big FROM product_photos GROUP BY id_product) AS ps
ON (p.id_prod = ps.id_product)

这是一个非常小的fiddle:http://sqlfiddle.com/#!9/aae09/7

GROUP_CONCAT 是特定于供应商的,不是 SQL 标准的一部分。在其他 DBMS 中,您应该查看 listagg (Oracle)、string_agg (Postgres) 等。最重要的要点:你不能 运行 你的代码不在不同的系统上修改。