How to SELECT cells from mysql for a HTML table? I have data for individual cells of a HTML (or excel) table as

CREATE TABLE table_cols
(
column_id smallint(5) unsigned NOT NULL AUTO_INCREMENT,
column_number tinyint(3) unsigned,
title varchar(255),
PRIMARY KEY(column_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_general_ci;

CREATE TABLE table_rows
(
row_id smallint(5) unsigned NOT NULL AUTO_INCREMENT,
row_number tinyint(3) unsigned,
title varchar(255),
PRIMARY KEY(row_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_general_ci;

CREATE TABLE table_cells
(
row_id smallint(5) unsigned,
column_id smallint(5) unsigned,
content varchar(255),
PRIMARY KEY(row_id,column_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_general_ci;


What is the best SELECT query to catch data to be displayed in a HTML table as

&lt;table&gt;

&lt;thead&gt; columns titles &lt;/thead&gt;

&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;row title&lt;/td&gt; cells &lt;/tr&gt;
&lt;/tbody&gt;

&lt;/table&gt;


I don't expert to make the entire table by one query. I get the results by  PHP.
MySql has no XML data type so it's hardly can help ( more help then app layer code) to generate HTML markup.
It can help to create ordered stream of cells. Kind of

select r.row_number, h.column_number, c.content
from table_cells c 
join table_rows r on c.row_id = r.row_d
join table_cols h on c.column_id = h.column_id
order by r.row_number, h.column_number


Or may be to convet sparse array of cells to regular one as directed by table_cols and table_rows, if that is the case.