2016-12-23 4 views
1

私は、MySQLの文の下に使用して行を取得しようとしています:選択クエリからの出力結果を分離あたりなど、主キー

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

私は:

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

私はフォーマット以下のデータを取得したいです以下のようにjava/mysql apiを使用していますが、私の要求に応じてどのように分離するかは、どのb_idにも1つずつ結果が与えられます。

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, %s\n", id, firstName); 
    } 

答えて

1

シンプルです。 b_id列とSQLステートメントの末尾にGROUP_CONCATtext

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

おかげで、私は最後にB_ID、GROUP BYを使用していたが、私は、1行のみを取得しています... – Anand

+1

は、「SET SESSION group_concat_max_len = 18446744073709551615を実行した後に働きました。 " – Anand

1

使用GROUP BY b_id、そしてあなたのSELECT部分​​にGROUP_CONCAT(',', desc.text)を使用することにより、グループ。

2

単純なクエリ:ポインタのための

SELECT b_id, GROUP_CONCAT(text SEPARATOR ',') as text FROM test1 GROUP BY b_id 
関連する問題