MySQL添加条件逻辑和组合SELECT

时间:2014-01-06 12:11:43

标签: java mysql sql

我希望通过将选择逻辑从Java转移到MySQL来简化我的代码,并且还希望将两个查询简化为单个查询。

我有两个表marriage_data,其中列player_id_1player_id_2 然后我player_dataplayer_idusername

给定方法中的整数,我想在列marriage_dataplayer_id_1中搜索player_id_2以获得匹配

如果我在player_id_1找到匹配项,我想获得player_id_2 如果我在player_id_2找到匹配项,我想获得player_id_1

检索到ID后,我想从username中选择player_data,其中检索到的ID等于player_id

目前我正在使用此代码来获得我想要的结果:

private String getSpouse(Connection conn, int id) throws SQLException {
    try (PreparedStatement ps = prepare(conn, "SELECT * FROM `marriage_data` WHERE `player_id_1` = ? OR `player_id_2` = ?", id, id)) {
        ResultSet marriageData = ps.executeQuery();
        while (marriageData.next()) {
            int one = marriageData.getInt("player_id_1");
            int two = marriageData.getInt("player_id_2");
            if (one > 0 && two > 0) {
                int spouseId = one != id ? one : two;
                try (PreparedStatement ps2 = prepare(conn, "SELECT * FROM `player_data` WHERE `player_id` = ?", spouseId)) {
                    ResultSet rs2 = ps2.executeQuery();
                    while (rs2.next()) {
                        return rs2.getString("username");
                    }
                }
            }
        }
    }
    return null;
}

我真的需要从这个混乱的java + 2mysql查询转移到单个MySQL查询中。任何指导或帮助将不胜感激。

编辑:

`player_data` structure
+-----------------+------------------+------+-----+---------+----------------+
| Field           | Type             | Null | Key | Default | Extra          |
+-----------------+------------------+------+-----+---------+----------------+
| player_id       | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| username        | varchar(32)      | NO   |     | NULL    |                |

`marriage_data` structure
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| player_id_1 | int(11)          | NO   | MUL | NULL    |                |
| player_id_2 | int(11)          | NO   |     | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+

Sample data from `player_data`
+-----------+-------------+
| player_id |  username   |
|     48616 |  player2    |
+-----------+-------------+
| player_id |  username   |
|     69054 |  player1    |

Sample data from `marriage_data`

+-----+-------------+-------------+
| id  | player_id_1 | player_id_2 |
+-----+-------------+-------------+
| 233 |       48616 |       69054 |
+-----+-------------+-------------+

在提供的样本中:

鉴于48616,它应该返回player1

鉴于69054,它应该返回player2

2 个答案:

答案 0 :(得分:0)

根据代码,您可以执行以下操作:

SELECT * FROM `marriage_data` AS md
  JOIN `player_data` AS pd1 ON pd1.player_id = md_player_id_1 
  JOIN `player_data` AS pd2 ON pd2.player_id = md_player_id_2 
 WHERE `md.player_id_1` = ? OR `md.player_id_2` = ?

基本上,将marriage_data加入player_data两次,一次加入播放器1,再加入播放器2.请记住,这可能需要一些推文。你真的不应该做SELECT *,但由于你没有显示任何表格结构,我不知道那里有哪些列。而不是SELECT *,你应该只指定你需要的列。此外,您可能会得到不明确的列,这会产生错误。

使用新的合并SELECT,您应该能够将WHILE循环简化为一个抓住所有内容的循环。

答案 1 :(得分:0)

使用以下查询。将ENTER_YOUR_PLAYERID_HERE替换为输入的玩家ID

select m.player_id_1 as 'original_player_name',m.player_id_2 as 'matched_player' ,p.username as 'input_user_name', 
(select username from player_data where player_id=matched_player) as 'matched_player_name' 
from marriage_data m inner join player_data p on p.player_id=m.player_id_1
and m.player_id_1=ENTER_YOUR_PLAYERID_HERE
union
select m.player_id_2 as 'original_player_name',m.player_id_1 as 'matched_player' ,p.username as 'input_user_name', 
(select username from player_data where player_id=matched_player) as 'matched_player_name' 
from marriage_data m inner join player_data p on p.player_id=m.player_id_2
and m.player_id_2=ENTER_YOUR_PLAYERID_HERE