选择子查询中的where子句

时间:2018-06-29 13:33:53

标签: mysql sql

有6张桌子

tours_atp (_ wta-相同)

players_atp (_ wta-相同) enter image description here

games_atp (_ wta-相同)

enter image description here

有了tours_atp + games_atp,一切都可以了,但是我如何才能加入(或在select里面)玩家表中包含玩家名称的接收列,像这样

ID1_G, ID2_G, player1, player2, id_t.....

我尝试选择内部联接

SELECT DISTINCT `ID_T_G` AS `id_t`, `NAME_T` AS `name`, `DATE_G`, `RESULT_G`, `DATE_T`, `COUNTRY_T`, `ID1_G`, `ID2_G`, `ID_R_G`,
  (SELECT CONCAT(`NAME`, ' ', `LASTNAME`) AS player1 FROM players_ru_atp WHERE games_atp.ID1_G = players_ru_atp.ID_P) AS `player1`,
  (SELECT CONCAT(`NAME`, ' ', `LASTNAME`) AS player2 FROM players_ru_atp WHERE games_atp.ID2_G = players_ru_atp.ID_P) AS `player2`
FROM `games_atp` LEFT JOIN `tours_atp` ON tours_atp.ID_T = games_atp.ID_T_G
WHERE (`DATE_G` BETWEEN '2018-06-29' AND '2018-06-29')
      AND (`player1` LIKE '%John Doe%') GROUP BY `ID_T_G`;

Unknown column 'player1' in 'where clause'

1 个答案:

答案 0 :(得分:1)

尝试一下。但是您的GROUP BY没有道理。

SELECT DISTINCT `ID_T_G` AS `id_t`
    , `NAME_T` AS `name`
    , `DATE_G`, `RESULT_G`
    , `DATE_T`, `COUNTRY_T`
    , `ID1_G`, `ID2_G`
    , `ID_R_G`
    , (SELECT CONCAT(`NAME`, ' ', `LASTNAME`) AS player1 FROM players_ru_atp WHERE games_atp.ID1_G = players_ru_atp.ID_P) AS `player1`
    , (SELECT CONCAT(`NAME`, ' ', `LASTNAME`) AS player2 FROM players_ru_atp WHERE games_atp.ID2_G = players_ru_atp.ID_P) AS `player2`
FROM `games_atp` 
LEFT JOIN `tours_atp` ON tours_atp.ID_T = games_atp.ID_T_G
WHERE `DATE_G` BETWEEN '2018-06-29' AND '2018-06-29'
      AND (SELECT CONCAT(`NAME`, ' ', `LASTNAME`) AS player1 FROM players_ru_atp WHERE games_atp.ID1_G = players_ru_atp.ID_P) LIKE '%John Doe%'
GROUP BY `ID_T_G`;