根据主键将输出结果隔离

  • 本文关键字:输出 结果 隔离 java mysql
  • 更新时间 :
  • 英文 :


我正在尝试使用以下mysql语句获得行:

+-----------------+
| text   | b_id   |
+-----------------|
|  a     |    1   |                                                                                                                                                                             
|  b     |    1   |                                                                                                                                                               
|  c     |    1   |                                                                                                                                                                              
|  e     |    2   | 
|  f     |    2   |

我想以低于格式获取数据:

 +---------------+
 | b_id  | Text  |
 +-------+-------+
 |  1    | a,b,c |
 |  2    | e,f   |

我正在使用下面的java/mysql api,但是它为任何b_id逐一提供了结果,如何根据我的要求将其隔离,任何提示都会有用。

 Connection conn = new SqlServiceImpl().getConnection("hostName/dbName?",
        "user", "pwd", "");
  String query =
        "select desc.text,desc.b_id from desc,(select b_id,short_desc from bids where product_id=999) as bi where bi.b_id= desc.bug_id LIMIT 50;";
  Statement st = conn.createStatement();
  ResultSet rs = st.executeQuery(query);
 while (rs.next())
  {
     int id = rs.getInt("b_id");
     String firstName = rs.getString("text");
     System.out.format("%s, %sn", id, firstName);
  }

简单查询:

SELECT b_id, GROUP_CONCAT(text SEPARATOR ',') as text FROM test1 GROUP BY b_id

简单。b_id列组和GROUP_CONCAT text

select
  b_id,
  group_concat(text separator ',') text
from my_table
group by b_id;

在SQL语句末尾使用GROUP BY b_id,并在选择部分中使用GROUP_CONCAT(',', desc.text)

最新更新