通过多个联接

时间:2017-01-08 15:06:02

标签: mysql

我花了好几天时间,这似乎是一个简单的问题。我希望有些善良的人可以直截了当。

我有一个包含最近赛马信息的数据库。我的问题涉及4个表:

EVENTS, RUNNERS, 课程, 价格

对于特定会议,例如赫里福德于1月4日,我试图让最喜欢的博彩公司赢得每场比赛。我可以制作收藏列表,我可以制作每场比赛的详细信息,但我无法将它们放在一起。

所以,这产生了每个人的最爱:

SELECT tbl_events.EVENT_ID, MIN(BSP)
from tbl_events
INNER JOIN tbl_prices ON tbl_prices.EVENT_ID = tbl_events.EVENT_ID
INNER JOIN tbl_courses ON tbl_courses.courseID = tbl_events.courseID
WHERE EVENT_DT LIKE '2017-01-04%'
AND place = "Here"
GROUP BY tbl_events.EVENT_ID;   

+-----------+----------+
| EVENT_ID  | MIN(BSP) |
+-----------+----------+
| 128981330 |     6.00 |
| 128981335 |     3.30 |
| 128981340 |     2.40 |
| 128981345 |     2.90 |
| 128981350 |     2.28 |
| 128981355 |     1.46 |
| 128981360 |     6.07 |
+-----------+----------+

这会产生我所有的其他信息:

SELECT tbl_events.EVENT_ID,tbl_events.EVENT_DT,country,place,SELECTION_NAME,  BSP
from tbl_events
INNER JOIN tbl_courses ON tbl_courses.courseID = tbl_events.courseID
INNER JOIN tbl_prices ON tbl_prices.EVENT_ID = tbl_events.EVENT_ID
INNER JOIN tbl_runners ON tbl_runners.runnersID = tbl_prices.runnersID
WHERE EVENT_DT LIKE '2017-01-04%'
AND place = "Here";

(LIMITED TO 10 just for illustration)

+-----------+---------------------+---------+-------+-------------------+---  -----+
| EVENT_ID  | EVENT_DT            | country | place | SELECTION_NAME    | BSP       |
+-----------+---------------------+---------+-------+-------------------+----  ----+
| 128981330 | 2017-01-04 12:55:00 | GB      | Here  | Rainbow Haze      |   13.50 |
| 128981330 | 2017-01-04 12:55:00 | GB      | Here  | Thats Gonna Sting |   7.37 |
| 128981330 | 2017-01-04 12:55:00 | GB      | Here  | Raduis Bleu       | 124.38 |
| 128981330 | 2017-01-04 12:55:00 | GB      | Here  | Staunton          |   6.00 |
| 128981330 | 2017-01-04 12:55:00 | GB      | Here  | Tisfreetdream     | 111.71 |
| 128981330 | 2017-01-04 12:55:00 | GB      | Here  | Flemi Two Toes    |  14.00 |
| 128981330 | 2017-01-04 12:55:00 | GB      | Here  | Young Lou         |  14.76 |
| 128981330 | 2017-01-04 12:55:00 | GB      | Here  | Act Four          |  25.00 |
| 128981330 | 2017-01-04 12:55:00 | GB      | Here  | Late Shipment     |   7.23 |
| 128981330 | 2017-01-04 12:55:00 | GB      | Here  | Celtic Tune       |  12.27 |
+-----------+---------------------+---------+-------+-------------------+--- -  ----+

我能做的就是把两件事放在一起,以产生每个事件的最爱。我已经尝试将第一个查询转换为子查询,我尝试了各种方法将查询加入到自身中,我尝试将第一个查询作为第二个查询中where子句的一部分进行嵌入

我几乎可以阅读每篇文章,并查看各种教程。有很多例子可以在同一个表格中为一组记录返回最小值,但我无法通过加入各种表格将其中的任何一个转换为我尝试做的事情。

表创建(根据要求):

| tbl_events | CREATE TABLE `tbl_events` (
 `EVENT_ID` int(11) NOT NULL,
 `EVENT_NAME` varchar(45) DEFAULT NULL,
 `EVENT_DT` datetime DEFAULT NULL,
 `courseID` int(11) NOT NULL,
 PRIMARY KEY (`EVENT_ID`),
 KEY `courseID_idx` (`courseID`),
 CONSTRAINT `courseID` FOREIGN KEY (`courseID`) REFERENCES `tbl_courses`       (`courseID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |

| tbl_prices | CREATE TABLE `tbl_prices` (
`priceID` int(11) NOT NULL AUTO_INCREMENT,
`EVENT_ID` int(11) NOT NULL,
`runnersID` int(11) NOT NULL,
`BSP` decimal(6,2) NOT NULL,
`PPMAX` decimal(6,2) NOT NULL,
`PPMIN` decimal(6,2) NOT NULL,
`IPMAX` decimal(6,2) NOT NULL,
`IPMIN` decimal(6,2) NOT NULL,
`MORNINGTRADEDVOLUME` decimal(15,2) NOT NULL,
`PPTRADEDVOL` decimal(15,2) NOT NULL,
`IPTRADEDVOL` decimal(15,2) NOT NULL,
PRIMARY KEY (`priceID`),
UNIQUE KEY `EVENTandRunnerIndex` (`EVENT_ID`,`runnersID`),
KEY `runnersID_idx3` (`runnersID`),
KEY `EVENT_ID_idx3` (`EVENT_ID`),
CONSTRAINT `EVENT_ID2` FOREIGN KEY (`EVENT_ID`) REFERENCES `tbl_events`      (`EVENT_ID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `runnersID2` FOREIGN KEY (`runnersID`) REFERENCES `tbl_runners`   (`runnersID`) ON DELETE NO ACTION ON UPDATE NO ACTION
 )   ENGINE=InnoDB AUTO_INCREMENT=747879 DEFAULT CHARSET=utf8 |

| tbl_runners | CREATE TABLE `tbl_runners` (
`runnersID` int(11) NOT NULL AUTO_INCREMENT,
`SELECTION_ID` varchar(45) NOT NULL,
`SELECTION_NAME` varchar(45) NOT NULL,
`EVENT_ID` int(11) NOT NULL,
PRIMARY KEY (`runnersID`),
UNIQUE KEY `SELECTION_NAME_UNIQUE` (`SELECTION_NAME`),
KEY `EVENT_ID_idx` (`EVENT_ID`)
)   ENGINE=InnoDB AUTO_INCREMENT=749692 DEFAULT CHARSET=utf8 |

| tbl_courses | CREATE TABLE `tbl_courses` (
`courseID` int(11) NOT NULL AUTO_INCREMENT,
`country` varchar(45) NOT NULL,
`place` varchar(45) NOT NULL,
PRIMARY KEY (`courseID`),
UNIQUE KEY `uniquecourseINDEX` (`country`,`place`)
) ENGINE=InnoDB AUTO_INCREMENT=300620 DEFAULT CHARSET=utf8 |

1 个答案:

答案 0 :(得分:0)

好的,对此做了一些更多的工作,我现在有一个可行的模型,基于我随后在后续查询中包含的视图。所以,

ALTER VIEW favourites AS 
SELECT tbl_events.EVENT_ID, MIN(BSP) AS fav
FROM tbl_events
INNER JOIN tbl_prices ON tbl_events.EVENT_ID = tbl_prices.EVENT_ID
INNER JOIN tbl_courses ON tbl_courses.courseID = tbl_events.courseID
WHERE EVENT_DT LIKE '2017-01-04%'
AND place = 'Here'
GROUP BY tbl_events.EVENT_ID;

然后:

   SELECT tbl_events.EVENT_ID,tbl_events.EVENT_DT,country,place,SELECTION_NAME,  BSP
   from tbl_events 
   INNER JOIN tbl_courses ON tbl_courses.courseID = tbl_events.courseID
   INNER JOIN tbl_prices ON tbl_prices.EVENT_ID = tbl_events.EVENT_ID
   INNER JOIN tbl_runners ON tbl_runners.runnersID = tbl_prices.runnersID
   INNER JOIN favourites ON tbl_events.EVENT_ID = favourites.EVENT_ID AND     tbl_prices.BSP = favourites.fav
   where EVENT_DT LIKE '2017-01-04%'
   AND place = "Here"
   GROUP BY tbl_events.EVENT_ID;

产生我想要的东西:

+-----------+---------------------+---------+-------+----------------+------+
| EVENT_ID  | EVENT_DT            | country | place | SELECTION_NAME | BSP  |
+-----------+---------------------+---------+-------+----------------+------+
| 128981330 | 2017-01-04 12:55:00 | GB      | Here  | Staunton       | 6.00  |
| 128981335 | 2017-01-04 13:25:00 | GB      | Here  | Canoodle       | 3.30 |
| 128981340 | 2017-01-04 13:55:00 | GB      | Here  | Charmix        | 2.40 |
| 128981345 | 2017-01-04 14:25:00 | GB      | Here  | Working Title  | 2.90 |
| 128981350 | 2017-01-04 14:55:00 | GB      | Here  | Jameson        | 2.28 |
| 128981355 | 2017-01-04 15:25:00 | GB      | Here  | Strong Pursuit | 1.46 |
| 128981360 | 2017-01-04 15:55:00 | GB      | Here  | Keel Haul      | 6.07 |
+-----------+---------------------+---------+-------+----------------+------+

我如何嵌套这两个查询,以便一次性产生最终结果?