<table>
<tr>
<td>1</td>
<td>bla-bla-1</td>
</tr>
<tr>
<td>1</td>
<td>bla-bla-2</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>2</td>
<td>bla-bla-3</td>
</tr>
<tr>
<td>2</td>
<td>bla-bla-4</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>3</td>
<td>bla-bla-3</td>
</tr>
</table>
INSERT INTO `table` (`id`, `a1`, `b1`) VALUES
(1, 1, 'bla-bla-1'),
(2, 1, 'bla-bla-2'),
(3, 2, 'bla-bla-3'),
(4, 2, 'bla-bla-4'),
(5, 3, 'bla-bla-3');
<?php
setlocale(LC_ALL, 'ru_RU.UTF-8');
require 'include/config.php';
global $connection;
$html = '<table>'."\r\n";
$query = "SELECT * FROM `table`";
$result = mysqli_query( $connection, $query );
if (!$result) {
printf("Error: %s\n", mysqli_error($connection));
exit();
}
$last_value='';
while ( $item = mysqli_fetch_array( $result ) ) {
if ($item['a1'] == $last_value) {
$html = $html.'<tr>'."\r\n";
$html = $html.'<td> </td>'."\r\n";
$html = $html.'<td> </td>'."\r\n";
$html = $html.'</tr>'."\r\n";
}
$html = $html.'<tr>'."\r\n";
$html = $html.'<td>'.$item['a1'].'</td>'."\r\n";
$html = $html.'<td>'.$item['b1'].'</td>'."\r\n";
$html = $html.'</tr>'."\r\n";
$last_value = $item['a1']; //можно добавить в else, но можно и так.
}
$html = $html.'</table>'."\r\n";
echo $html;
?>
<table>
<tr>
<td>1</td>
<td>bla-bla-1</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>1</td>
<td>bla-bla-2</td>
</tr>
<tr>
<td>2</td>
<td>bla-bla-3</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>2</td>
<td>bla-bla-4</td>
</tr>
<tr>
<td>3</td>
<td>bla-bla-3</td>
</tr>
</table>
<tr>
<td>1</td>
<td>bla-bla</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>1</td>
<td>bla-bla-2</td>
</tr>
$last_value='';
while ( $item = mysqli_fetch_array( $result ) ) {
if ( $item['a1'] == $last_value) {
$html. = '<tr>'."\r\n";
$html. = '<td>'.$item['a1'].'</td>'."\r\n";
$html. = '<td>'.$item['b1'].'</td>'."\r\n";
$html. = '</tr>'."\r\n";
$html. = '<tr>'."\r\n";
$html. = '<td> </td>'."\r\n";
$html. = '<td> </td>'."\r\n";
$html. = '</tr>'."\r\n";
$last_value=$item['a1'];
}
else {
$html. = '<tr>'."\r\n";
$html. = '<td>'.$item['a1'].'</td>'."\r\n";
$html. = '<td>'.$item['b1'].'</td>'."\r\n";
$html. = '</tr>'."\r\n";
$last_value=$item['a1'];
}
}
$last_value='';
while ( $item = mysqli_fetch_array( $result ) ) {
if ( $item['a1']!=$last_value) {
$html. = '<tr>'."\r\n";
$html. = '<td> </td>'."\r\n";
$html. = '<td> </td>'."\r\n";
$html. = '</tr>'."\r\n";
$html. = '<tr>'."\r\n";
$html. = '<td>'.$item['a1'].'</td>'."\r\n";
$html. = '<td>'.$item['b1'].'</td>'."\r\n";
$html. = '</tr>'."\r\n";
$last_value=$item['a1'];
}
else {
$html. = '<tr>'."\r\n";
$html. = '<td>'.$item['a1'].'</td>'."\r\n";
$html. = '<td>'.$item['b1'].'</td>'."\r\n";
$html. = '</tr>'."\r\n";
$last_value=$item['a1'];
}
}
SELECT `u`.* FROM `u`
LEFT JOIN ( SELECT `PERSCODE`, `CODEM` FROM `u` GROUP BY `PERSCODE`, `CODEM` HAVING COUNT(*) > 1 ) AS `utemp`
ON ( `utemp`.`PERSCODE` = `u`.`PERSCODE` AND SUBSTRING(`utemp`.`CODEM`, 1, 3) = SUBSTRING(`u`.`CODEM`, 1, 3) )
WHERE `utemp`.`PERSCODE` IS NOT NULL ORDER BY `u`.`PERSCODE`
GROUP BY `PERSCODE`, `CODEM` HAVING COUNT(*) > 1
SUBSTRING(`u`.`CODEM`, 1, 3) )