简体   繁体   English

努力将WHERE子句添加到INNER JOIN

[英]Struggling adding WHERE clause to INNER JOIN

I have a query which is supposed to select the lowest price_per_pax_after_tax from every backend_hotels_id date_start and package_supplier and this appears to be working until I add a WHERE clause. 我有一个查询,应该从每个backend_hotels_id date_startpackage_supplier选择最低的price_per_pax_after_tax ,在我添加WHERE子句之前,这似乎一直有效。

Here's the query: 这是查询:

SELECT e.price_per_pax_after_tax, e.hotel_score, e.package_id, e.package_type
FROM packages_sorted_YQU e
INNER JOIN (
  SELECT db_id, MIN( price_per_pax_after_tax ) AS lowest_price, package_id, hotel_score
  FROM `packages_sorted_YQU`
  WHERE `package_type` IN ('9', '10', '18') 
  AND `package_duration` IN ('6', '8', '12')
  GROUP BY 
    `date_start` , `package_supplier` , `backend_hotels_id`
) AS j 
ON j.db_id = e.db_id
AND j.lowest_price= e.price_per_pax_after_tax
AND j.hotel_score = e.hotel_score
AND j.package_id = e.package_id;

The table is huge but all of the fields listed are INT except for date_start which is DATE 该表很大,但是列出的所有字段都是INT,但date_start为DATE

The where clause causing the problem is: 引起问题的where子句是:

WHERE `package_type` IN ('9', '10', '18') 
AND `package_duration` IN ('6', '8', '12')

Without the where clause I get over 400 results and with the where clause I get zero results :( Any help will be very much appreciated. 如果没有where子句,我将得到400多个结果,而有了where子句,我将得到零结果:(任何帮助将不胜感激。

If your columns package_type and package_duration are of type int you don't have to wrap the values inside ' like strings. 如果您的列package_typepackage_durationint类型,则不必将值包装在'之内'例如字符串。

SELECT e.price_per_pax_after_tax, e.hotel_score, e.package_id, e.package_type
FROM packages_sorted_YQU e
INNER JOIN (
  SELECT db_id, MIN( price_per_pax_after_tax ) AS lowest_price, package_id, hotel_score
  FROM `packages_sorted_YQU`
  WHERE `package_type` IN (9, 10, 18) 
  AND `package_duration` IN (6, 8, 12)
  GROUP BY 
    `date_start` , `package_supplier` , `backend_hotels_id`
) AS j 
ON j.db_id = e.db_id
AND j.lowest_price= e.price_per_pax_after_tax
AND j.hotel_score = e.hotel_score
AND j.package_id = e.package_id;

The subquery: 子查询:

  SELECT db_id
       , MIN( price_per_pax_after_tax ) AS lowest_price
       , package_id
       , hotel_score
  FROM `packages_sorted_YQU`
  WHERE `package_type` IN ('9', '10', '18') 
  AND `package_duration` IN ('6', '8', '12')
  GROUP BY 
       `date_start` 
     , `package_supplier` 
     , `backend_hotels_id`

will yield indeterminate results, with or without the WHERE clause. 无论是否带有WHERE子句,都会产生不确定的结果。 Because you are grouping by date_start, package_supplier, backend_hotels_id and have in the SELECT list columns without any aggregate functions on them: db_id, package_id, hotel_score . 因为您date_start, package_supplier, backend_hotels_id分组date_start, package_supplier, backend_hotels_id并且在SELECT列表列中没有任何聚合函数: db_id, package_id, hotel_score

This query should work consistently if the (date_start, package_supplier, backend_hotels_id) is the Primary Key or Unique. 如果(date_start, package_supplier, backend_hotels_id)是“主键”或“唯一” (date_start, package_supplier, backend_hotels_id)则此查询应保持一致。

Which is the PRIMARY KEY of the table and are there any other UNIQUE keys? 该表的PRIMARY KEY是哪个,还有其他UNIQUE键吗?

Hi all and thank you for your valuable input. 大家好,谢谢您的宝贵意见。 I've solved the problem without a sub-query and it works a bit faster too. 我已经解决了没有子查询的问题,并且它的工作速度也更快。

SELECT MIN
(
    concat
    (
        LPAD(`price_per_pax_after_tax` , 5, '0'),
        LPAD(`package_id` , 12, '0'),
        LPAD(`hotel_score` , 7, '0')
    )
) AS cat
FROM `packages_sorted_YQU`
WHERE `package_type` IN
(
    9, 10, 18
)
AND `package_duration` IN
(
    6, 7, 8
)
GROUP BY `date_start` , `package_supplier` , `backend_hotels_id`

Then in PHP I break apart the concatenation with: 然后在PHP中,我用以下方法分解串联:

while($r=mysql_fetch_array($q,MYSQL_ASSOC))
{
    $a[lrp][] = intval(substr($r[cat], 0, 5));
    $a[package_id][] = intval(substr($r[cat], 5, 12));
    $a[hotel_score][] = substr($r[cat], 17, 7);
}

I was lucky that the only FLOAT value was the hotel_score so I put that last - the other two were of type INT 我很幸运,唯一的FLOAT值是hotel_score所以我把它放在最后-其他两个是INT类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM