这是一个延续从昨天这个问题 。
这里是我的三个表:
战士表
fighter_id | name
-----------------------
1 | John
2 | Steve
3 | Bill
4 | Bobby
事件表
event_id | event_name | event_date
-------------------------------------------
1 | MMA | 01/01/2010
2 | Cool | 02/20/2010
3 | Yeaa! | 04/15/2010
战斗表
fight_id | fighter_a | fighter_b | winner | method | event
-----------------------------------------------------------------------
1 | 1 | 2 | 1 | Sub | 1
2 | 4 | 1 | 4 | KO | 2
3 | 1 | 3 | NULL | Draw | 3
结果试图让
result | opponent | method | event | date
----------------------------------------------------------
Draw | Bill | Draw | Yeaa! | 04/15/2010
Loss | Bobby | KO | Cool | 02/20/2010
Win | Steve | Sub | MMA | 01/01/2010
所以在打架表,fighter_a,fighter_b和赢家是对应于战斗机表fighter_id整数。
我基本上是检索基于fighter_id(fighter_id $)数据的页面上。
我试图与战斗机,其中包括他的对手的名字,结果(赢,损失,画,或NC),方法,EVENT_NAME和EVENT_DATE的每次吵架创建行。 我们面临的挑战是,赢家可以在任fighter_a或fighter_b。 这并不总是在同一列。 我感谢所有帮助我能。
select
fight_id,
CASE
WHEN winner is not null and winner=fighter_id then 'win'
WHEN winner is not null and winner<>fighter_id then 'loss'
WHEN winner is null and method='Draw' then 'draw'
WHEN winner is null and method = 'No Contest' then 'no contest'
ELSE ''
END as match_result,
participant.name 'participant',
opponent.name 'opponent'
FROM fights
INNER JOIN fighters as participant on participant.fighter_id = fights.fighter_a
INNER JOIN fighters as oppoent on opponent.fighter_id = fights.fighter_b
WHERE
fighter_a=$fighter_id OR fighter_b=$fighter_id
ORDER BY
event_date DESC
使用子查询的用条件来进行切换,你正在寻找一个在column_a,如果它在column_b的fighter_id,这样一来,它简化了您的操作,并在外部查询联接:
SELECT
(
CASE
WHEN a.winner = a.f_a THEN 'Win'
WHEN a.winner = a.f_b THEN 'Loss'
WHEN a.winner IS NULL THEN a.method
END
) AS result,
b.name AS opponent,
a.method AS method,
c.event_name AS event,
c.event_date AS date
FROM
(
SELECT
IF(fighter_b = $fighter_id, fighter_b, fighter_a) AS f_a,
IF(fighter_b = $fighter_id, fighter_a, fighter_b) AS f_b,
winner,
method,
event
FROM
fights
WHERE
$fighter_id IN (fighter_a, fighter_b)
) a
INNER JOIN
fighters b ON a.f_b = b.fighter_id
INNER JOIN
events c ON a.event = c.event_id
ORDER BY
c.event_date DESC
另外,如果获奖者字段为空,然后就呼应法领域。 这样一来,当你想去的地方的赢家是空到您的系统中添加另一种类型的方法,你不必让更多的条件检查会附上你的CASE
语句。
你可以使用一个联盟:
SELECT CASE WHEN winner = participant_id THEN 'Win'
WHEN winner = opponent_id then 'Loss'
ELSE method
END AS result, fighters.name AS opponent, method, event_name, event_date
FROM fights
INNER JOIN fighters ON fighter_id = opponent_id
INNER JOIN events ON event = event_id
WHERE participant_id = $fighter_id
UNION ALL
SELECT CASE WHEN winner = participant_id THEN 'Loss'
WHEN winner = opponent_id then 'Win'
ELSE method
END AS result, fighters.name AS opponent, method, event_name, event_date
FROM fights
INNER JOIN fighters ON fighter_id = participant_id
INNER JOIN events ON event = event_id
WHERE opponent_id = $fighter_id
ORDER BY event_date DESC
你可以只修改你CASE
语句,以配合$fighter_id
。
所以,你的CASE
语句是-
select fight_id,
CASE
WHEN winner is not null and winner = f.fighter_id then 'win'
WHEN winner is not null and winner <> f.fighter_id then 'loss'
WHEN winner is null and method='Draw' then 'draw'
WHEN winner is null and method = 'No Contest' then 'no contest'
ELSE ''
END as match_result,
f.name AS participant,
(SELECT o.name FROM fighters o WHERE o.fighter_id = IF(f.fighter_id = fs.fighter_a, fs.fighter_b, fs.fighter_a)) AS opponent
FROM fights fs,
fighters f
WHERE f.fighter_id = $fighter_id
AND (fs.fighter_a = f.fighter_id OR fs.fighter_b = f.fighter_id)
SELECT
CASE
WHEN winner is not null and winner=fighter_id then 'win'
WHEN winner is not null and winner<>fighter_id then 'loss'
WHEN winner is null and method='Draw' then 'draw'
WHEN winner is null and method = 'No Contest' then 'no contest'
ELSE ''
END result,
f2.name opponent,
method,
event_name event,
event_date date
FROM fights f1
INNER JOIN fighters f2
ON f1.fighter_b = f2.fighter_id
INNER JOIN events e
ON e.event_id = f1.event
ORDER BY date DESC