首先感谢您阅读本文。
我想在2个表之间进行查询,但我不知道该怎么做。
我有一个名为products
的表,另一个名为product_photos
。我想查询所有products
并在结果的每个通道中添加表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个字段,我在表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都是结果,而不仅仅是第一个结果。
谢谢!
答案 0 :(得分:0)
您可以使用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);
这将为您提供完整的产品照片。
答案 1 :(得分:0)
从最终编辑来看,我认为这就是你想要的。它使用GROUP_CONCAT
将链接表中的所有值连接在一起。
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
答案 2 :(得分:0)
在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)
这是一个非常小的小提琴:http://sqlfiddle.com/#!9/aae09/7
GROUP_CONCAT
是特定于供应商的,不是SQL标准的一部分。在其他DBMS中,您应该查看listagg
(Oracle),string_agg
(Postgres),依此类推。最重要的内容:如果不对不同系统进行修改,则无法运行代码。