MySQL:从多个表中选择全部

时间:2015-11-25 09:31:28

标签: mysql

每张桌子都有一个玩家号码。我想在一个结果中获取所有表中的所有列。即例如,对于每日行,其他表中的所有列都是0.像这样:

enter image description here

表:

CREATE TABLE `daily` (
 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
 `playerNumber` varchar(255) DEFAULT '',
 `benchpress` varchar(255) DEFAULT '');

CREATE TABLE `weekly` (
 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
 `playerNumber` varchar(255) DEFAULT '',
 `sprint40y` varchar(255) DEFAULT '',
 `sprint20y` varchar(255) DEFAULT '');

CREATE TABLE `monthly` (
 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
 `playerNumber` varchar(255) DEFAULT '',
 `verticalJump` varchar(255) DEFAULT '',
 `sprint300y` varchar(255) DEFAULT '',
 `ldrill` varchar(255) DEFAULT '');

SQL查询:

SELECT *
FROM
  daily d,weekly w,monthly m
LEFT JOIN
  daily d.playerNumber ON (d.playerNumber = w.playerNumber) AND  daily d.playerNumber ON (d.playerNumber = m.playerNumber)
UNION
  SELECT *
FROM
  daily d,weekly w,monthly m
RIGHT JOIN
  daily d.playerNumber ON (d.playerNumber = w.playerNumber) AND  daily d.playerNumber ON (d.playerNumber = m.playerNumber)

1 个答案:

答案 0 :(得分:0)

SELECT `playerNumber`,
        `benchpress`,
        IFNULL(`sprint40y`,0) AS benchpress,
        IFNULL(`sprint20y`,0) AS sprint20y,
        IFNULL(`verticalJump`,0) AS verticalJump,
        IFNULL(`sprint300y`,0) AS sprint300y,
        IFNULL(`ldrill`,0) AS ldrill

FROM daily 
LEFT JOIN weekly ON daily.`playerNumber`=weekly.`playerNumber`
LEFT JOIN monthly ON daily.`playerNumber`=monthly.`playerNumber`

希望这有帮助。