SQL Query Joining 5 tables

4

I need help to make a query (multiple relationships) in my WebService + MySQL to return the result in my Android application.

I have these tables:

Query

Ineedtoqueryaperson(tb_suspeito),withtheattributes(Name,cpfandRg).

PreparedStatementstatement=con.prepareStatement("SELECT...);

The return I need in my Android application is a table in this format:

Question

Is the SQL query with SELECT and JOINs all done in a single statement query? How to proceed? Would creating views be a solution to feature on Android later?

    
asked by anonymous 30.10.2014 / 13:23

4 answers

3

Just use simple JOINs between the tables, the next few times, do as @Caffe said and add your attempts and show some effort, search the site, we have excellent questions about joins such as @Bacco in this question is practically a complete article on the subject.

With subqueries

SELECT
  A.descricao_artigo,
  P.dt_processo
  SP.descricao_situacao_processo
  P.pdf_Processo
FROM
  processo_judicial P
  INNER JOIN tp_situacao_processo SP on P.cd_situacao_processo = SP.cd_situacao_processo
  INNER JOIN tp_artigo A ON O.cd_artigo = A.cd_artigo
WHERE
  P.cd_processo IN (
    SELECT cdProcesso FROM suspeito_Processo WHERE cd_suspeito IN (
      SELECT cd_suspeito FROM suspeito WHERE (/*SUAS condicao de suspeito*/)
    )
  )

JOIN in all Tables

SELECT
  A.descricao_artigo,
  P.dt_processo
  SP.descricao_situacao_processo
  P.pdf_Processo
FROM
  processo_judicial P
  INNER JOIN tp_situacao_processo SP on P.cd_situacao_processo = SP.cd_situacao_processo
  INNER JOIN tp_artigo A ON O.cd_artigo = A.cd_artigo
  INNER JOIN suspeito_Processo SUP ON (SUP.cd_processo = P.cd_processo)
  INNER JOIN suspeito S ON (S.cd_suspeito = SUP.cd_suspeito)
WHERE
  WHERE /* SUAS CONDICOES DE SUSPEIRO USANDO O ALIAS S */

Using EXISTS

SELECT
  A.descricao_artigo,
  P.dt_processo
  SP.descricao_situacao_processo
  P.pdf_Processo
FROM
  processo_judicial P
  INNER JOIN tp_situacao_processo SP on P.cd_situacao_processo = SP.cd_situacao_processo
  INNER JOIN tp_artigo A ON O.cd_artigo = A.cd_artigo
WHERE
  EXISTS (
    SELECT 1 
    FROM
      suspeito_processo SUP
      INNER JOIN suspeito S ON S.cd_suspeito = SUP.cd_suspeito
    WHERE
      SUP.cd_processo = P.cd_processo AND
      /* SUAS CONDICOES DO SUSPEITO COM ALIAS S
    )

All these options are valid solutions

    
30.10.2014 / 14:08
3

One or more queries

If you have a specific goal to perform a query to get certain information organized in a certain way, yes, the most effective way is a single query to the database that returns everything ready to use:

Inquiry

Assuming you want to query for suspeito X:

SELECT
  tp_artigo.descricao_artigo AS descricao_artigo,
  processo_judicial.dt_processo AS dt_processo,
  tp_situacao_processo.descricao_situacao_processo AS descricao_situacao,
  processo_judicial.pdf_processo AS pdf_processo
FROM suspeito
INNER JOIN suspeito_processo ON (
  suspeito_processo.cd_suspeito = suspeito.cd_suspeito
)
INNER JOIN processo_judicial ON (
  processo_judicial.num_processo = suspeito_processo.num_processo
)
INNER JOIN tp_situacao_processo ON (
  tp_situacao_processo.cd_situacao_processo = processo_judicial.cd_situacao_processo
)
INNER JOIN tp_artigo ON (
  tp_artigo.cd_artigo = processo_judicial.cd_artigo
)
WHERE suspeito.cd_suspeito = 1

Result of the query

The query above performs a specific task which is to collect the following data for suspeito X:

┌──────────────────┬─────────────┬────────────────────┬──────────────┐
│ descricao_artigo │ dt_processo │ descricao_situacao │ pdf_processo │
└──────────────────┴─────────────┴────────────────────┴──────────────┘

Web service or MySQL View

If the query is to get general information, ie for all suspeito , a VIEW is preferable because it becomes more practical to update in the future and also because there are no variable data to consider.

If the query is as it was understood, query certain information of suspeito X, then the web service will be the path to take because of the logic and validations to make to the data. / p>

Note: You can also have a VIEW to receive parameters, but for this you need to create a MySQL function. Too much work and code to keep, where it is also preferable here to keep the query in web service .

    
30.10.2014 / 14:24
1
SELECT a.descricao_artigo, p.dt_processo, sp.descricao_situacao_processo, p.pdf_Processo
FROM processo_judicial AS p
INNER JOIN tp_situacao_processo as sp on p.cd_situacao_processo = sp.cd_situacao_processo
INNER JOIN tp_artigo as a ON p.cd_artigo = a.cd_artigo
INNER JOIN suspeito_processo AS susp_proc ON p.num_processo = susp_proc.num_processo
INNER JOIN suspeito AS s ON susp_proc.cd_suspeito = s.cd_suspeito
WHERE s.nome = //NOME// AND s.cpf_suspeito = //CPF// AND s. rg_suspeito = //RG//;
    
30.10.2014 / 14:23
0
public ArrayList<ProcessoJudicial> consultar(String nome_suspeito, String cpf_suspeito, String rg_suspeito){

    ArrayList<ProcessoJudicial> results = new ArrayList<ProcessoJudicial>();

    try{

        Connection con = ConectaMySQL.obterConexao ();
        PreparedStatement statement = con.prepareStatement ("SELECT tpa.descricao_artigo, tsp.descricao_situacao_processo FROM processo_judicial AS pj INNER JOIN tp_situacao_processo as tsp on tsp.cd_situacao_processo = pj.cd_situacao_processo  INNER JOIN tp_artigo as tpa ON tpa.cd_artigo = pj.cd_artigo INNER JOIN suspeito_processo AS sproc ON sproc.num_processo = pj.num_processo INNER JOIN suspeito AS s ON s.cd_suspeito = sproc.cd_suspeito WHERE s.nome_suspeito = '"+nome_suspeito+"' AND s.cpf_suspeito = '"+cpf_suspeito+"' AND s.rg_suspeito = '"+rg_suspeito+"'");

        ResultSet result = statement.executeQuery ();

        while (result.next()){
            ProcessoJudicial obj = new ProcessoJudicial();
            obj.setDescricao_artigo(result.getString("descricao_artigo"));
            obj.setDescricao_situacao_processo(result.getString("descricao_situacao_processo"));                
            results.add(obj);
        }
        result.close();
        statement.close();
        return results;
    }
    catch ( Exception e){
        e.printStackTrace ();
    }
    return null;
}

I was able to test it in SOAPUI, it just needs to work in my application and generate the table in Android. The body of the response envelope looks like this.

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
  <ns:consultarResponse xmlns:ns="http://wsprototipo.br.com" xmlns:ax21="http://wsprototipo.br.com/xsd">
     <ns:return xsi:type="ax21:ProcessoJudicial" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <ax21:descricao_artigo>Lesão Corporal</ax21:descricao_artigo>
        <ax21:descricao_situacao_processo>Transito</ax21:descricao_situacao_processo>
        <ax21:num_processo xsi:nil="true"/>
     </ns:return>
  </ns:consultarResponse>

    
31.10.2014 / 01:18