我尝试使用UNION加入以下SELECT语句,但最终我在同一列上获得了所有结果。 但是,我希望每个查询的结果都在一个单独的列中。
SELECT Player_id,Game_id, R1
FROM poker_hands f, poker_cards a, poker_cards b
WHERE f.r1 = a.card_name
AND f.r2 = b.card_name
AND (f.hand_type = 'Four of kind'
AND (a.game_value != b.game_value)
UNION ALL
/* R2 */
SELECT Player_id,Game_id, R2
FROM poker_hands f, poker_cards a, poker_cards b
WHERE f.r1 = a.card_name
AND f.r2 = b.card_name
AND (f.hand_type = 'High hand'
AND (a.game_value != b.game_value);
我希望输出类似
Player_id | Game_id | R1 | R2
0001 | 0002 | | 8S
0002 | 0003 | 9H |
0003 | 0004 | 5C | 5S
0004 | 0005 | | 6D
答案 0 :(得分:0)
假设您的其余查询是正确的,请以这种方式使用它。第一次查询中null
的硬编码R2
和null
的硬编码R1
是第二次查询。
SELECT Player_id,Game_id, R1 , '' as R2
FROM poker_hands f, poker_cards a, poker_cards b
WHERE f.r1 = a.card_name
AND f.r2 = b.card_name
AND (f.hand_type = 'Four of kind'
AND (a.game_value != b.game_value)
UNION ALL
/* R2 */
SELECT Player_id,Game_id, '' as R1, R2
FROM poker_hands f, poker_cards a, poker_cards b
WHERE f.r1 = a.card_name
AND f.r2 = b.card_name
AND (f.hand_type = 'High hand'
AND (a.game_value != b.game_value);
答案 1 :(得分:0)
您可以使用case
statement
,请尝试以下操作:
SELECT Player_id,Game_id, CASE WHEN f.hand_type = 'Four of kind' THEN R1 ELSE '' END as R1,
CASE WHEN f.hand_type = 'High hand' THEN R2 ELSE '' END AS R2
FROM poker_hands f, poker_cards a, poker_cards b
WHERE f.r1 = a.card_name
AND f.r2 = b.card_name
AND (a.game_value != b.game_value AND (f.hand_type = 'High hand' OR f.hand_type = 'Four of kind' ));