Mysql Can';我想不出是什么';我的小组有什么问题吗

Mysql Can';我想不出是什么';我的小组有什么问题吗,mysql,Mysql,我有以下两个表格: Table `Products` ProductId (INT) PrimaryKey Name (VARCHAR(25)) Description (VARCHAR(255)) Table `Images` ImageId (INT) PrimaryKey ImagePath (VARCHAR(255)) ImageDescription (VARCHAR(255)) products_Pro

我有以下两个表格:

 Table `Products`
 ProductId   (INT) PrimaryKey
 Name        (VARCHAR(25))
 Description (VARCHAR(255))

 Table `Images`
 ImageId            (INT) PrimaryKey
 ImagePath          (VARCHAR(255))
 ImageDescription   (VARCHAR(255))
 products_ProductId (INT)
Images
表包含与特定产品关联的图像。它与
Products
表是一对多关系,因此一个产品可以有多个图像。唯一可以为空的列(目前大多数情况下都是空的)是
Images.ImageDescription
。我想选择一个产品列表,并在同一个查询中,得到他们的所有图像以及。我编写了以下查询:

SELECT P.*, 
       GROUP_CONCAT(DISTINCT CONCAT(I.ImagePath, '@', I.ImageDescription) SEPARATOR ',') AS _ProductImages 
FROM (SELECT * FROM Products WHERE ProductId IN (1,2,3,4,5,6,7)) as P 
LEFT JOIN Images as I ON I.products_ProductId = P.ProductId
GROUP BY P.ProductId

所有选定的产品在Images表中至少有1个关联行,前3个在Images表中有3个关联行,但是,当查询运行并返回时,
\u ProductImages
在每一行中都为空。有人能指出我做错了什么吗?

当你把一个字符串连接成NULL时,结果是NULL。因此:

CONCAT(I.ImagePath, '@', COALESCE(I.ImageDescription, ''))

将字符串与NULL连接时,结果为NULL。因此:

CONCAT(I.ImagePath, '@', COALESCE(I.ImageDescription, ''))

除了托尔斯滕的回答之外:

除了
CONCAT()
之外,您还可以使用
CONCAT\u WS()
。阅读更多关于它的信息

它可以很好地处理空值,并且省略了不必要的分隔符

例如:

mysql > select concat('whatever', '@', NULL);
+-------------------------------+
| concat('whatever', '@', NULL) |
+-------------------------------+
| NULL                          |
+-------------------------------+
1 row in set (0.00 sec)

mysql > select concat_ws('@', 'whatever', NULL);
+----------------------------------+
| concat_ws('@', 'whatever', NULL) |
+----------------------------------+
| whatever                         |
+----------------------------------+
1 row in set (0.00 sec)

mysql > select concat_ws('@', 'whatever', 'whatever');
+----------------------------------------+
| concat_ws('@', 'whatever', 'whatever') |
+----------------------------------------+
| whatever@whatever                      |
+----------------------------------------+
1 row in set (0.00 sec)
根据托尔斯滕的回答,你会得到:

mysql > select concat('whatever', '@', COALESCE(NULL, ''));
+---------------------------------------------+
| concat('whatever', '@', COALESCE(NULL, '')) |
+---------------------------------------------+
| whatever@                                   |
+---------------------------------------------+
1 row in set (0.00 sec)

除了托尔斯滕的回答之外:

除了
CONCAT()
之外,您还可以使用
CONCAT\u WS()
。阅读更多关于它的信息

它可以很好地处理空值,并且省略了不必要的分隔符

例如:

mysql > select concat('whatever', '@', NULL);
+-------------------------------+
| concat('whatever', '@', NULL) |
+-------------------------------+
| NULL                          |
+-------------------------------+
1 row in set (0.00 sec)

mysql > select concat_ws('@', 'whatever', NULL);
+----------------------------------+
| concat_ws('@', 'whatever', NULL) |
+----------------------------------+
| whatever                         |
+----------------------------------+
1 row in set (0.00 sec)

mysql > select concat_ws('@', 'whatever', 'whatever');
+----------------------------------------+
| concat_ws('@', 'whatever', 'whatever') |
+----------------------------------------+
| whatever@whatever                      |
+----------------------------------------+
1 row in set (0.00 sec)
根据托尔斯滕的回答,你会得到:

mysql > select concat('whatever', '@', COALESCE(NULL, ''));
+---------------------------------------------+
| concat('whatever', '@', COALESCE(NULL, '')) |
+---------------------------------------------+
| whatever@                                   |
+---------------------------------------------+
1 row in set (0.00 sec)

当您将
左连接
更改为
内部连接
时会发生什么?在
图像
记录中,
图像路径
和/或
图像描述
可以为空吗?@Thorsten Kettner我已经编辑了这篇文章,并为您的问题添加了答案
ImageDescription
可以为空,并且在当前大多数测试用例中都为空。@Psi
Internal JOIN
会产生相同的结果。好吧,这是一个旁注:当您看到保证每个产品至少有一个图像时,为什么还要使用outer JOIN?要查找数据不一致?当您将
左连接
更改为
内部连接
时会发生什么情况?在
图像
记录中,
图像路径
和/或
图像描述
可以为空吗?@Thorsten Kettner我已经编辑了这篇文章,并为您的问题添加了答案
ImageDescription
可以为空,并且在当前大多数测试用例中都为空。@Psi
Internal JOIN
会产生相同的结果。好吧,这是一个旁注:当您看到保证每个产品至少有一个图像时,为什么还要使用outer JOIN?找到数据不一致?是的,这解决了它,谢谢!会在几分钟内接受:)是的,这解决了它,谢谢!将在几分钟内接受:)