Tuesday 15 June 2010

php - MySQL multiple join, multiple sort maintain group -


I'm trying to display data from four tables. I have three one-to-many relationships from these tables. I need to group certain data according to a shared suit_id and I should be able to sort the fields of different tables independently. I should be able to sort more than one field from different tables.

  • This data represents legal cases (suit) from court records.
  • There are probably many parties for each suit.
  • For any party only there will be a fee and that type of party will be "Defendant".

    DB Structure:

    !

    The following is my current query that gives correct data. The data is properly classified by suit_id but the layoffs are off and my query does not allow me to control the sorting.

      SELECT s.suit_id, s.case_number, p.party_id, p name, c.charge_id, c.charge_code FROM suction_party sp LEFT JOIN suits sp.suit_id = s.suit_id LEFT JOIN (SELECT PP. * From Party PP WHERE pp.party_type! = 'Bond unit' ORDER BY pp.last_name DESC) sp.party_id on p. P.party_id LEFT JOIN Charge C on p.party_id = c.party_id WHERE p.party_id SP No Sp.party_id, sp.suit_id GROUP by GROUP   

    sample output

      + --------- + ------------- + ------- --- + ------------------ + ------ ----- + ------------- + | Suit_id | Case_number | Party_id | Name | In charge_ID | Chargecode | + --------- + ------------- + ---------- + -------------- ---- + ----------- + ------------- + | 1 | 66519GE 1 | Tony T. | 1 | 262061253 | | 1 | 66519GE 2 | Law, officer Faucet Faucet | 2 | 2013 A262076 | 3 | Bad, Bobby B2. 0528 | | 2 | 2013 A262076 | 5 | Police, peter | Faucet Faucet | 3 | A357654 KK 6 | Crook, Kimberly | 3 | 2143 | | 3 | A357654 KK 7 | Crime, Captain | Faucet Faucet | 4 | B357654 kk 8 | Crook, Kimberly | 4 | 0660 | | 4 | B357654 kk 9 | Law, officer Faucet Faucet + --------- + ------------- + ---------- + -------------- ---- + ----------- + ------------- +   

    I want to be able while suiting_id Sorting by name and chargecode properly grouping.

    The problem with your command is that you sp Party_id . The pair specifically defines each line, so no other sort criteria is necessary.

    I think this will fix your problem:

      by command sp.suit_id, p.name, C.charge_code   < / Div> 

No comments:

Post a Comment