我试图从两个表中获取数据并将它们链接在一起,但我只是看不到使查询正常工作。这是我一直在运行的查询,以及我正在寻找的表格外观。
select playerid, sid, s.s_name, s.s_namelink, s.minsec FROM
((select max(points) as pts,
(
select gd.date
from game_stats_lakers t1, game_details gd
where t1.playerid = t.playerid and t1.sid = t.sid and t1.gid = gd.gid
order by t1.points desc limit 1
) as pts_date) UNION ALL (select points as pts, points_date as pts_date from game_high_reg)) as tot, from seasons s, game_stats_lakers t where t.playerid = 3 s.sid = t.sid group by playerid, sid
我的桌子是什么样的
Table 1 - game_stats_lakers
playerid sid pts pts_date
3 0304r 43 2003-11-23
3 0405r 40 2005-03-07
3 0910r 38 2009-12-23
Table 2 - game_high_reg
playerid sid points points_date
3 0506r 35 2006-03-23
3 0708r 34 2008-03-12
3 0809r 38 2009-02-23
What I'm Looking For
playerid sid pts pts_date
3 0304r 43 2003-11-23
3 0405r 40 2005-03-07
3 0506r 35 2006-03-23
3 0708r 34 2008-03-12
3 0809r 38 2009-02-23
3 0910r 38 2009-12-23
谢谢您的建议。
答案 0 :(得分:1)
这将提供您想要的结果:
SELECT * FROM game_stats_lakers WHERE playerid = 3
UNION
SELECT * FROM game_high_reg WHERE playerid = 3
ORDER BY pts_date;
CREATE TABLE game_stats_lakers ( `playerid` INTEGER, `sid` VARCHAR(8), `pts` INT, `pts_date` date ); INSERT INTO game_stats_lakers (`playerid`, `sid`, `pts`, `pts_date`) VALUES ('3', '0304r' , '43', '2003-11-23'), ('3', '0405r' , '40', '2005-03-07'), ('3', '0910r' , '38', '2009-12-23');
✓ ✓
CREATE TABLE game_high_reg ( `playerid` INTEGER, `sid` VARCHAR(8), `points` INT, `points_date` date ); INSERT INTO game_high_reg (`playerid`, `sid`, `points`, `points_date`) VALUES ('3', '0506r' , '35', '2006-03-23'), ('3', '0708r' , '34', '2008-03-12'), ('3', '0809r' , '38', '2009-02-23');
✓ ✓
SELECT * FROM game_stats_lakers WHERE playerid = 3 UNION SELECT * FROM game_high_reg WHERE playerid = 3 ORDER BY pts_date;
playerid | sid | pts | pts_date -------: | :---- | --: | :--------- 3 | 0304r | 43 | 2003-11-23 3 | 0405r | 40 | 2005-03-07 3 | 0506r | 35 | 2006-03-23 3 | 0708r | 34 | 2008-03-12 3 | 0809r | 38 | 2009-02-23 3 | 0910r | 38 | 2009-12-23
db <>提琴here
答案 1 :(得分:0)
我通过使用union all命令解决了它。这不是世界上最漂亮的东西,但它确实可以正常工作。该表有很多列。感谢所有人为我指出正确的方向。一旦我真正想到了,实际上就很容易。
select r.playerid, r.sid, se.s_name, se.s_namelink, se.minsec, se.lsbox, r.points as pts, r.points_date as pts_date, r.minutes as min, r.mp_date as min_date, r.seconds as sec, r.fgm, r.fgm_date, r.fga, r.fga_date, r.3ptm, r.3ptm_date, r.3pta, r.3pta_date, r.ftm, r.ftm_date, r.fta, r.fta_date, r.offr, r.offr_date, r.defr, r.defr_date, r.totr, r.totr_date, r.assists, r.assists_date as ast_date, r.steals, r.steals_date as stl_date, r.blocks, r.blocks_date as blk_date, r.turnovers, r.turnovers_date as to_date, r.personal_fouls, r.personal_fouls_date as pf_date from game_high_reg r, seasons se where r.playerid = 3 and r.sid LIKE '%r%' and r.sid = se.sid
union all
select
t.playerid,
t.sid,
s.s_name,
s.s_namelink,
s.minsec,
s.lsbox,
max(points) pts,
(
select gd.date
from game_stats_lakers t1, game_details gd
where t1.playerid = t.playerid and t1.sid = t.sid and t1.gid = gd.gid
order by t1.points desc limit 1
) pts_date,
max(min) min,
(
select gd.date
from game_stats_lakers t1, game_details gd
where t1.playerid = t.playerid and t1.sid = t.sid and t1.gid = gd.gid
order by t1.min desc limit 1
) min_date,
(
select t1.sec
from game_stats_lakers t1
where t1.playerid = t.playerid and t1.sid = t.sid
order by t1.min desc limit 1
) sec,
max(fgm) fgm,
(
select gd.date
from game_stats_lakers t1, game_details gd
where t1.playerid = t.playerid and t1.sid = t.sid and t1.gid = gd.gid
order by t1.fgm desc limit 1
) fgm_date,
max(fga) fga,
(
select gd.date
from game_stats_lakers t1, game_details gd
where t1.playerid = t.playerid and t1.sid = t.sid and t1.gid = gd.gid
order by t1.fga desc limit 1
) fga_date,
max(3ptm) 3ptm,
(
select gd.date
from game_stats_lakers t1, game_details gd
where t1.playerid = t.playerid and t1.sid = t.sid and t1.gid = gd.gid
order by t1.3ptm desc limit 1
) 3ptm_date,
max(3pta) 3pta,
(
select gd.date
from game_stats_lakers t1, game_details gd
where t1.playerid = t.playerid and t1.sid = t.sid and t1.gid = gd.gid
order by t1.3pta desc limit 1
) 3pta_date,
max(ftm) ftm,
(
select gd.date
from game_stats_lakers t1, game_details gd
where t1.playerid = t.playerid and t1.sid = t.sid and t1.gid = gd.gid
order by t1.ftm desc limit 1
) ftm_date,
max(fta) fta,
(
select gd.date
from game_stats_lakers t1, game_details gd
where t1.playerid = t.playerid and t1.sid = t.sid and t1.gid = gd.gid
order by t1.fta desc limit 1
) fta_date,
max(offr) offr,
(
select gd.date
from game_stats_lakers t1, game_details gd
where t1.playerid = t.playerid and t1.sid = t.sid and t1.gid = gd.gid
order by t1.offr desc limit 1
) offr_date,
max(defr) defr,
(
select gd.date
from game_stats_lakers t1, game_details gd
where t1.playerid = t.playerid and t1.sid = t.sid and t1.gid = gd.gid
order by t1.defr desc limit 1
) defr_date,
max(totr) totr,
(
select gd.date
from game_stats_lakers t1, game_details gd
where t1.playerid = t.playerid and t1.sid = t.sid and t1.gid = gd.gid
order by t1.totr desc limit 1
) totr_date,
max(assists) assists,
(
select gd.date
from game_stats_lakers t1, game_details gd
where t1.playerid = t.playerid and t1.sid = t.sid and t1.gid = gd.gid
order by t1.assists desc limit 1
) ast_date,
max(steals) steals,
(
select gd.date
from game_stats_lakers t1, game_details gd
where t1.playerid = t.playerid and t1.sid = t.sid and t1.gid = gd.gid
order by t1.steals desc limit 1
) stl_date,
max(blocks) blocks,
(
select gd.date
from game_stats_lakers t1, game_details gd
where t1.playerid = t.playerid and t1.sid = t.sid and t1.gid = gd.gid
order by t1.blocks desc limit 1
) blk_date,
max(turnovers) turnovers,
(
select gd.date
from game_stats_lakers t1, game_details gd
where t1.playerid = t.playerid and t1.sid = t.sid and t1.gid = gd.gid
order by t1.turnovers desc limit 1
) to_date,
max(personal_fouls) personal_fouls,
(
select gd.date
from game_stats_lakers t1, game_details gd
where t1.playerid = t.playerid and t1.sid = t.sid and t1.gid = gd.gid
order by t1.personal_fouls desc limit 1
) pf_date
from game_stats_lakers t, seasons s where t.playerid = 3 and t.sid LIKE '%r%' and t.sid = s.sid
group by playerid, s_name;