Do you mean something like this?
<?php
// get the "name" headings that you need for the columns
// and also use them as keys in a "template" array
//
$res = $db->query("SELECT DISTINCT name
FROM dataset
ORDER BY name
");
$names = $res->fetchAll();
$heads = array_column($names, 'name');
$temp = array_fill_keys($heads, '');
$table_header = "<tr><td></td><td class='thead'>Result</td><td class='thead'>" . join("</td><td class='thead'>", $heads) . "</td></tr>\n";
// now get the data
// store in an array by "id"
// witd subarrays for each name
$res = $db->query("SELECT
id
, edate
, result
, name
, nos
FROM maintab m
JOIN dataset d ON m.id = d.mid
ORDER BY id
");
$data = [];
foreach ($res as $r) {
if (!isset($data[$r['id']])) {
$data[$r['id']] = [ 'edate' => $r['edate'],
'result' => $r['result'],
'names' => $temp // the template array from earlier
];
}
$data[$r['id']]['names'][$r['name']] = $r['nos']; // put value in tempate array
}
// now we simply output data array into html table rows
$tdata = '';
foreach ($data as $row) {
$tdata .= "<tr><td>{$row['edate']}</td><td>{$row['result']}</td><td>" . join('</td><td>', $row['names']) . "</td></tr>\n";
}
?>
<html>
<head>
<title>Example</title>
<style type='text/css'>
td {
padding: 4px 10px;
}
.thead {
font-weight: 600;
border-top: 1px solid gray;
border-bottom: 1px solid gray;
}
</style>
</head>
<body>
<table>
<?= $table_header ?>
<?= $tdata ?>
</table>
</body>
</html>
OUTPUT
[edit] PS Sorry about the data typo. That's what happens when people post pictures instead of copyable text.