Jump to content


Photo

mysql distinct with multiple column


  • Please log in to reply
4 replies to this topic

#1 Yohanne

Yohanne

    Advanced Member

  • Members
  • PipPipPip
  • 185 posts

Posted 06 April 2014 - 10:07 PM

Hi 

 

im going to create a report and i have multiple rows of data and im going to distinct some part of my column and in my html table im going call colspan to display correctly. image below is the example should i want to display in my report.

 

image.png

 

 please help i need mysql way solutions and sample would be great. 

 

Thanks..



#2 trq

trq

    Advanced Member

  • Administrators
  • 31,032 posts
  • LocationSydney, Australia.

Posted 06 April 2014 - 11:52 PM

What is the problem?

http://thorpesystems.com | http://proemframework.org | http://github.com/trq

SmtpCatcher - A very simple mock sendmail useful for testing PHP mail scripts.
OPM - My Linux package manager.


#3 Yohanne

Yohanne

    Advanced Member

  • Members
  • PipPipPip
  • 185 posts

Posted 07 April 2014 - 12:13 AM

the problem is, i don't get the correct query to distinct of the said column. i need sample mysql code base on image above. please

 

this code is badly not work.. 

("SELECT DISTINCT region FROM branch ('SELECT semployees.empid,employees.username,employees.fname,branch.branch_id,branch.brach_name,branch.regions,code_tss.tss_id,code_tss.empid,code_tss.tss_trans_num,code_tss.branch_id,code_tss.tss_date
FROM code_tss INNER JOIN branch ON code_tss.branch_id = branch.branch_id 
INNER JOIN employees ON code_tss.empid = employees.empid ORDER BY `tss_id` DESC LIMIT $vpb_start_page, $vpb_page_limit')") 


#4 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,604 posts
  • LocationCheshire, UK

Posted 07 April 2014 - 06:32 AM

Keep the query simple, store data in an array by region then output from the array.

 

Example code (sorry I couldn't rotate the table though)

<?php
$db = new mysqli(HOST,USERNAME,PASSWORD,'test'); // use your credentials
error_reporting(-1);

//
// CREATE TEST DATA
//
$db->query("DROP TABLE IF EXISTS jayson");
$sql = "CREATE TABLE jayson (
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    f1 VARCHAR(20),
    f2 VARCHAR(20),
    f3 VARCHAR(20),
    f4 VARCHAR(20)
    )";
$db->query($sql);
$sql = "INSERT INTO jayson (f1,f2,f3,f4) VALUES 
    ('Region 1', 'Place 1', 'adadassd', 'cxxvxv'),
    ('Region 1', 'Place 2', 'kjlkl', 'hsdhsdfjsh'),
    ('Region 1', 'Place 3', 'lkjlklkklk', 'uooio'),
    ('Region 2', 'Place 4', 'qeqewq', 'vbnvbnvnvbv'),
    ('Region 2', 'Place 5', 'aaaaaaaa', 'fvrvrfv'),
    ('Region 2', 'Place 6', 'gdgdgdfgdf', 'hntntnhntn'),
    ('Region 2', 'Place 7', 'efefefe', 'jmjmmymym')
    ";
$db->query($sql);

//
// QUERY THE TABLE AND STORE IN ARRAY BY REGION (f1)
//
$data = array();
$sql = "SELECT f1,f2,f3,f4
        FROM jayson
        ORDER BY f1,f2";
$res = $db->query($sql);
while ($row = $res->fetch_assoc()) {
    $data[$row['f1']][] = array_slice($row,1);
}

?>
<html>
<head>
<meta name="generator" content="PhpED 12.0 (Build 12010, 64bit)">
<title>Example</title>
<meta name="author" content="Barand">
<meta name="creation-date" content="04/07/2014">
<style type='text/css'>
th {
    font-family: sans-serif;
    font-size: 10pt;
    background-color: blue;
    color: white;
}
td {
    font-family: sans-serif;
    font-size: 8pt;
    background-color: #eee;
    color: black;
}
table {
    border-collapse: collapse;
    width: 400px;
}
</style>
</head>
<body>
    <table border='1' cellpadding='5'>
        <tr>
            <th>F1</th>
            <th>F2</th>
            <th>F3</th>
            <th>F4</th>
        </tr>
        
        <?php
            // OUTPUT THE DATA ARRAY
            foreach ($data as $reg => $recs) {
                $k = count($data[$reg]); // get rowspan
                for ($i=0; $i<$k; $i++) {
                    echo "<tr>";
                    if ($i==0) {
                        echo "<td rowspan='$k'>$reg</td>";
                    }
                    echo "<td>" . join("</td><td>", $recs[$i]) . "</td></tr>\n";
                }
            }
        ?>
    </table>
</body>
</html>


moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#5 Yohanne

Yohanne

    Advanced Member

  • Members
  • PipPipPip
  • 185 posts

Posted 07 April 2014 - 08:38 PM

Hi barand..

 

really wow and it work great and i know im very close by. and if you observe my posted above, i have 3 tables to join  and now i have no idea if i need to create another query to get the result from 3 table and to pass it by to your $query you create?

 

and i need the result from..

('SELECT code_tss.tss_id,
	 code_tss.branch_id,
	 code_tss.tss_date,
	 code_tss.tss_trans_num,
		branch.branch_id,
		branch.brach_name,
		branch.regions,
			employees.empid,
			employees.fname FROM code_tss 
	INNER JOIN branch ON code_tss.branch_id = branch.branch_id 
	    INNER JOIN employees ON code_tss.empid = employees.empid WHERE tss_date = "April 5, 2014" ORDER BY regions,brach_name')

result

 

+++++++  regions  ++++++  brach_name  +++++++   tss_trans_num   +++++++   fname   ++++++++






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com