Mysql添加别名,带有并发症

时间:2011-03-10 11:52:22

标签: mysql subquery alias add

所以我建立了一个预订系统,我在一些价格计算方面遇到了一些问题。

最初我有

SELECT c_id, c_title, c_imgdrop, c_link, c_text4, 
    (SELECT SUM(bd_price) as price FROM booking_dates WHERE site_id = '15' AND bd_room_type IN ('single','double') AND bd_date IN ('2011-03-05') AND bd_h_id = c_id) AS sum_price
FROM c_content
JOIN (SELECT bd_h_id FROM booking_dates WHERE site_id = '15' AND bd_date IN ('2011-03-05') AND bd_available <= '1' AND bd_room_type = 'single' AND bd_price > '0' GROUP BY bd_h_id HAVING count(*) = '1' ) q_0 ON c_content.c_id = q_0.bd_h_id
JOIN (SELECT bd_h_id FROM booking_dates WHERE site_id = '15' AND bd_date IN ('2011-03-05') AND bd_available <= '1' AND bd_room_type = 'double' AND bd_price > '0' GROUP BY bd_h_id HAVING count(*) = '1' ) q_1 ON c_content.c_id = q_1.bd_h_id
WHERE site_id = '15'

哪个工作正常,但如果你尝试计算同一类型的多个房间类型它不能很好地工作

无论如何,我试图将查询拆分为房间类型,并将该值乘以预订中该类型的房间数量。我正在使用这个(Adding MySQL alias fields together),这意味着我必须添加额外级别的子查询,这会使事情变得混乱。

我现在几乎就在那里,但它没有识别我的子查询中的一个字段名,我期望它由于它执行查询的顺序。

我现在拥有的是:

SELECT c_id, c_title, c_imgdrop, c_link, c_text4, 
    (SELECT sum_price_single, sum_price_double, sum_price_single + sum_price_double AS sum_price FROM
        (SELECT
            ((SELECT SUM(bd_price) as price FROM booking_dates WHERE site_id = '15' AND bd_room_type = 'single' AND bd_date IN ('2011-03-05') AND bd_h_id = c_id) * 1) AS sum_price_single,
            ((SELECT SUM(bd_price) as price FROM booking_dates WHERE site_id = '15' AND bd_room_type = 'double' AND bd_date IN ('2011-03-05') AND bd_h_id = c_id) * 1) AS sum_price_double
            FROM booking_dates
        )
    x)
FROM c_content
JOIN (SELECT bd_h_id FROM booking_dates WHERE site_id = '15' AND bd_date IN ('2011-03-05') AND bd_available <= '1' AND bd_room_type = 'single' AND bd_price > '0' GROUP BY bd_h_id HAVING count(*) = '1' ) q_0 ON c_content.c_id = q_0.bd_h_id
JOIN (SELECT bd_h_id FROM booking_dates WHERE site_id = '15' AND bd_date IN ('2011-03-05') AND bd_available <= '1' AND bd_room_type = 'double' AND bd_price > '0' GROUP BY bd_h_id HAVING count(*) = '1' ) q_1 ON c_content.c_id = q_1.bd_h_id
WHERE site_id = '15'

并且给了我以下错误

  

'where'中的未知列'c_id'   条款“

有什么想法吗?这应该输出一个酒店列表(来自c_content表的值),以及每个酒店的价格。价格包括各种日期的总和,但也必须考虑到mtuliple房间和混合房间类型,也有不同的价格。

2 个答案:

答案 0 :(得分:1)

子选择可能是性能杀手,应尽可能避免。嵌套的子选择是邪恶的很多倍,并且会导致类似于您遇到的范围问题。我建议删除所有的子子选择,如果你有时间,重写查询,使其根本不需要子选择。

与此同时,我认为以下内容可以解决您的范围问题。

SELECT *, sum_price_single + sum_price_double AS sum_price FROM (
    SELECT c_id, c_title, c_imgdrop, c_link, c_text4,
        (SELECT SUM(bd_price) * 1 as price FROM booking_dates WHERE site_id = '15' AND bd_room_type = 'single' AND bd_date IN ('2011-03-05') AND bd_h_id = c_id) AS sum_price_single,
        (SELECT SUM(bd_price) * 1 as price FROM booking_dates WHERE site_id = '15' AND bd_room_type = 'double' AND bd_date IN ('2011-03-05') AND bd_h_id = c_id) AS sum_price_double
    FROM c_content
    JOIN (SELECT bd_h_id FROM booking_dates WHERE site_id = '15' AND bd_date IN ('2011-03-05') AND bd_available <= '1' AND bd_room_type = 'single' AND bd_price > '0' GROUP BY bd_h_id HAVING count(*) = '1' ) q_0 ON c_content.c_id = q_0.bd_h_id
    JOIN (SELECT bd_h_id FROM booking_dates WHERE site_id = '15' AND bd_date IN ('2011-03-05') AND bd_available <= '1' AND bd_room_type = 'double' AND bd_price > '0' GROUP BY bd_h_id HAVING count(*) = '1' ) q_1 ON c_content.c_id = q_1.bd_h_id
    WHERE site_id = '15'
) AS t1

基本上移动核心子选择两个级别,并将单个和双精度的“和”包装到外部选择中。要确认,这不是最佳的......但它应该可以解决您当前的问题。我可以在午餐后重新审视这个问题:P

答案 1 :(得分:0)

不是最终解决方案,因为喜欢Jeff Parkers,但这是我用来在短期内解决它的查询

SELECT c_id, c_title, c_imgdrop, c_link, c_text4,
((SELECT SUM(bd_price) as price FROM booking_dates WHERE site_id = '15' AND bd_room_type = 'single' AND bd_date IN ('2011-03-05') AND bd_h_id = c_id) * 1)
+
((SELECT SUM(bd_price) as price FROM booking_dates WHERE site_id = '15' AND bd_room_type = 'double' AND bd_date IN ('2011-03-05') AND bd_h_id = c_id) * 1) AS sum_price
FROM c_content
JOIN (SELECT bd_h_id FROM booking_dates WHERE site_id = '15' AND bd_date IN ('2011-03-05') AND bd_available <= '1' AND bd_room_type = 'single' AND bd_price > '0' GROUP BY bd_h_id HAVING count(*) = '1' ) q_0 ON c_content.c_id = q_0.bd_h_id
JOIN (SELECT bd_h_id FROM booking_dates WHERE site_id = '15' AND bd_date IN ('2011-03-05') AND bd_available <= '1' AND bd_room_type = 'double' AND bd_price > '0' GROUP BY bd_h_id HAVING count(*) = '1' ) q_1 ON c_content.c_id = q_1.bd_h_id
WHERE site_id = '15' ORDER BY sum_price desc