查询以逗号分隔列表显示一个列名

时间:2015-08-12 07:16:29

标签: mysql sql select

我在mysql数据库工作,我需要一些帮助,列出一个列数据值,以逗号分隔显示,实际上有两个表:一个是人员表,另一个是群组表,现在一个人可以属于许多组,所以我试图显示用户数据。

我想显示如下数据:

UserName                 Group            Email
User #1                  Grp1,Grp3,Grp5   user1@email.com
User #2                  Grp3,Grp1        user2@email.com

这是我的查询

select ft_smsender_groups as g left join (select p1.id,p2.group_id 
from ft_smsender_persons2groups as p2 inner join ft_smsender_persons 
as p1 on p1.id = p2.person_id ) as p2 on g.id = p2.group_id','g.id,g.name,
created,updated where 
deleted = 0 group by id,name

实际上涉及三个表:

ft_smsender_groups
ft_smsender_persons2groups - This table holds key from both the tables
ft_smsender_persons

2 个答案:

答案 0 :(得分:1)

您可以使用group_concat功能:

/**
 * 
 */

package itext.sample;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//import com.itextpdf.text.BaseColor;
//import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
//import com.itextpdf.text.Font;
//import com.itextpdf.text.Font.FontFamily;
//import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfReader;
//import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.DocWriter;

/**
 * @author prithvi
 *
 */
public class FirstPdf {

    private static final String Result = "D:/Eclipse Java/image.pdf";

    public static String main(String[] args) throws SQLException,IOException,DocumentException {
        try {

            Class.forName("com.mysql.jdbc.Driver");

        } catch (ClassNotFoundException e) {

            System.out.println("Where is your MySQL JDBC Driver?");
            e.printStackTrace();
            return null;

        }
      System.out.println("MySQL JDBC Driver Registered!");
        Connection connection = null;
        try {
            connection = DriverManager
                    .getConnection("jdbc:mysql://69.167.139.172/bluedb",
                            "color", "prithvi");

        } catch (SQLException e) {
            System.out.println("Connection Failed! Check output console");
            e.printStackTrace();
            return null;
        }

        if (connection != null) {
            System.out.println("You made it, take control your database now!");
        } else {
            System.out.println("Failed to make connection!");
        }
        // creating pdf document
    Document document = new Document();
    try {
          //writing to the outputfile
            PdfWriter writer= PdfWriter.getInstance(document,new FileOutputStream(Result)) .setInitialLeading(16);
            document.open(); //opening the document to do the action
            Statement stm = null;
            stm = connection.createStatement();//creating database query
            ResultSet rs = null;
            rs = stm.executeQuery("SELECT * FROM Sec1");
            PdfPTable table = new PdfPTable(2);
            PdfReader reader =new PdfReader ("D:/Eclipse Java/HiltonForms2014_r.pdf");
            AcroFields form = reader.getAcroFields();
            form.setField("LASTNAME", rs.getCursorName());
            int n = reader.getNumberOfPages();
            PdfImportedPage page;
            for( int i= 1; i <=n;i++)
            {
                page = writer.getImportedPage(reader,i);
                table.addCell(Image.getInstance(page));
            }
            document.add(table);
            document.close();
            connection.close();
            reader.close();


            /*while (rs.next()){

                        document.add(new Chunk(rs.getString(Result)));
                        document.add(new Chunk(""));
                        Font font = new Font(FontFamily.TIMES_ROMAN, 10,Font.BOLD, BaseColor.WHITE);
                        Chunk id = null;
                        id = new Chunk(rs.getString("Sec1ID"), font);
                        id.setBackground(BaseColor.BLACK, 1f, 0.5f, 1f , 1.5f);
                        id.setTextRise(6);
                        document.add(id);
                        document.add(Chunk.NEWLINE);
                        document.add(new Paragraph("hey there! you created a new pdf"));
                        stm.close();
                        connection.close();
                        document.close();
                    }*/



    }

                 catch (DocumentException | SQLException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }


}

答案 1 :(得分:1)

我认为您正在寻找GROUP_COUNCAT(your_filed_name)功能

http://dev.mysql.com/doc/refman/5.6/en/group-by-functions.html#function_group-concat