Yohanne Posted April 7, 2014 Share Posted April 7, 2014 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. please help i need mysql way solutions and sample would be great. Thanks.. Quote Link to comment https://forums.phpfreaks.com/topic/287573-mysql-distinct-with-multiple-column/ Share on other sites More sharing options...
trq Posted April 7, 2014 Share Posted April 7, 2014 What is the problem? Quote Link to comment https://forums.phpfreaks.com/topic/287573-mysql-distinct-with-multiple-column/#findComment-1475198 Share on other sites More sharing options...
Yohanne Posted April 7, 2014 Author Share Posted April 7, 2014 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')") Quote Link to comment https://forums.phpfreaks.com/topic/287573-mysql-distinct-with-multiple-column/#findComment-1475201 Share on other sites More sharing options...
Barand Posted April 7, 2014 Share Posted April 7, 2014 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> Quote Link to comment https://forums.phpfreaks.com/topic/287573-mysql-distinct-with-multiple-column/#findComment-1475224 Share on other sites More sharing options...
Yohanne Posted April 8, 2014 Author Share Posted April 8, 2014 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 ++++++++ Quote Link to comment https://forums.phpfreaks.com/topic/287573-mysql-distinct-with-multiple-column/#findComment-1475347 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.