mySQL查询不会返回预期的结果

时间:2017-03-22 15:44:24

标签: mysql

我对此查询有疑问:

SELECT *,
GROUP_CONCAT(COALESCE(STX_Amount, "0")) AS ApplicableTaxesRate,
GROUP_CONCAT(COALESCE(STX_TaxeName, "0")) AS ApplicableTaxesName
FROM ___BillableDatas
JOIN ___Invoices ON ___BillableDatas.BIL_BookingId=___Invoices.IVC_BookingId 
JOIN ___Bookings ON ___BillableDatas.BIL_BookingId=___Bookings.BOO_Id 
JOIN ___Inventory ON ___BillableDatas.BIL_Item=___Inventory.INV_Id 
LEFT JOIN ___SalesTaxes ON FIND_IN_SET(STX_Id, BIL_ApplicableTaxes) > 0 
WHERE BIL_HotelId='AAA00' AND IVC_Id=14 AND BIL_Id IN (IVC_Elements)
ORDER BY BIL_Id ASC

从这个sqlFiddle(http://sqlfiddle.com/#!9/b2016)和我的实际数据,这个查询只返回一行,而它应该返回3(3因为IVC_Elements包含3个不同的ID)。

-

所需的输出应如下:

|----------|----------|------------|----------|----------|--------------|--------------------|---------------------|
| BIL_Id   | IVC_Id   | BIL_Date   | BIL_Rate | BIL_Type | BIL_Quantity | INV_Name           | BIL_ApplicableTaxes |
|----------|----------|------------|----------|----------|--------------|--------------------|---------------------|
| 31       | 14       | 2017-03-15 | 130.00   | Night    | 1            |                    |                     |
| 32       | 14       | 2017-03-13 | 130.00   | Night    | 1            |                    |                     |
| 33       | 14       | 2017-03-17 | 130.00   | Night    | 1            |                    |                     |
| 45       | 14       | 2017-11-01 | 100.00   | Item     | 5            | Lit supplémentaire | 11                  |
| 50       | 14       | 2017-03-16 |  12.00   | Item     | 2            | Petit-déjeuner     |                     |
|----------|----------|------------|----------|----------|--------------|--------------------|---------------------|

请帮忙吗?

1 个答案:

答案 0 :(得分:3)

为什么WHERE子句上的IN不起作用

因为您在IVC_Elements上存储了一个逗号分隔的字符串,我很抱歉,但您不能以这种方式使用它:

 WHERE FIELD IN (FIELD_WITH_COMA_DELIMITED_VALUES); 
  

IN子句需要列表

如果您使用当前值' 31,32,33'它返回ID = 31,这是唯一可以评估的ID,但是尝试通过' 32,31,33'并且您将获得行ID = 32。

你如何解决它:改变它:

FIND_IN_SET(BIL_Id, IVC_Elements) > 0

以这种方式更改您的查询:

SELECT    BIL_Id, IVC_Id, DATE_FORMAT(BIL_Date, "%Y-%m-%d") BIL_Date, BIL_Rate, BIL_Type, 
          BIL_Quantity, COALESCE(INV_Name, '') INV_Name, BIL_ApplicableTaxes
FROM      ___BillableDatas
JOIN      ___Invoices  
ON        ___BillableDatas.BIL_BookingId = ___Invoices.IVC_BookingId
JOIN      ___Bookings  
ON        ___BillableDatas.BIL_BookingId = ___Bookings.BOO_Id
LEFT JOIN ___Inventory 
ON        ___BillableDatas.BIL_Item = ___Inventory.INV_Id
LEFT JOIN ___SalesTaxes 
ON        FIND_IN_SET(BIL_ApplicableTaxes, STX_Id) > 0
WHERE     BIL_HotelId='AAA00' 
AND       IVC_Id=14
AND       FIND_IN_SET(BIL_Id, IVC_Elements) > 0 
ORDER BY  BIL_Id ASC
;

结果如下:

+--------+--------+------------+----------+----------+--------------+----------+---------------------+
| BIL_Id | IVC_Id | BIL_Date   | BIL_Rate | BIL_Type | BIL_Quantity | INV_Name | BIL_ApplicableTaxes |
+--------+--------+------------+----------+----------+--------------+----------+---------------------+
| 31     | 14     | 2017-03-15 | 130,00   | Night    | 1            |          |                     |
+--------+--------+------------+----------+----------+--------------+----------+---------------------+
| 32     | 14     | 2017-03-16 | 130,00   | Night    | 1            |          |                     |
+--------+--------+------------+----------+----------+--------------+----------+---------------------+
| 33     | 14     | 2017-03-17 | 130,00   | Night    | 1            |          |                     |
+--------+--------+------------+----------+----------+--------------+----------+---------------------+

在此处查看:http://rextester.com/BMX51701