1

这是架构和示例查询的小提琴:http ://sqlfiddle.com/#!2/573ec4/9

我希望使用通过其他连接获得的 I.section 和 L.level 返回 C.price。当使用带有成本的内部连接时,我没有结果:http ://sqlfiddle.com/#!2/573ec4/2

由于成本是将部分和级别映射到价格的表,因此我希望能够计算查询中所有放映的价格

4

4 回答 4

1

当您加入Cost表时,您不包括正确的连接条件:

INNER JOIN `cost` `C` 
  ON `I`.`Section` = c.section  -- = c.section is missing
  AND `L`.`level` = c.level;  -- = c.level is missing

因此,您的完整查询将是:

SELECT  `F`.`date`,
  `F`.`time`,
  `F`.`tname`,
  `I`.`section`,
  `L`.`level`,
  c.price
FROM `booking_for_schedule` `F`
INNER JOIN `booking_in_seats` `I` 
  on `F`.`tname`=`I`.`tname` 
  AND `F`.`booking_num` = `I`.`booking_num`
INNER JOIN `level` `L` 
  on `F`.`date`=`L`.`date` 
  AND `F`.`time`=`L`.`time` 
  AND `F`.`tname`=`L`.`tname`
INNER JOIN `cost` `C` 
  ON `I`.`Section` = c.section
  AND `L`.`level` = c.level;

请参阅带有演示的 SQL Fiddle

于 2013-03-12T02:47:53.827 回答
0

看起来您缺少一些子句INNER JOIN cost C(当您加入Cost表格时)。

它应该如下所示:

INNER JOIN `cost` `C` ON `I`.`Section`=`C`.`Section` AND `L`.`level`=`C`.`level`

代替:

INNER JOIN `cost` `C` ON `I`.`Section` AND `L`.`level`
于 2013-03-12T02:46:51.020 回答
0

你错过了加入专栏。你没有结果的原因是因为I.Section AND L.level总是会返回false。

SELECT  F.date,
        F.time,
        F.tname,
        I.section,
        L.level,
        c.*
FROM    booking_for_schedule F
        INNER JOIN booking_in_seats I 
            ON  F.tname = I.tname AND 
                F.booking_num = I.booking_num
        INNER JOIN level L 
            ON  F.date = L.date AND 
                F.time = L.time AND 
                F.tname = L.tname
        INNER JOIN cost C 
            ON  I.Section = C.Section AND  -- <<== HERE
                L.level = C.level          -- <<== HERE
于 2013-03-12T02:48:23.570 回答
0

你的最后一行是问题:

INNER JOIN `cost` `C` ON `I`.`Section` AND `L`.`level`

应该是这样的:

INNER JOIN `cost` `C` ON `I`.`Section` = `C`.`Section` AND `L`.`level` = `C`.`Level`
于 2013-03-12T02:55:05.880 回答