Table of Contents
How to create a table from a MySQL query in wpDataTables
Say your HR system stores the company’s employees in a MySQL database, and you want a staff directory on your intranet with each person’s position and start date, always up to date. You don’t need to export anything. You write one SQL query that returns the rows you want, and wpDataTables shows its result as a table that sorts, filters and searches, and reads fresh data on every page load.
This page assumes you know basic SQL and can test a query in a tool such as phpMyAdmin. If you don’t, build the query with the MySQL query constructor instead. Writing queries for you isn’t part of plugin support.
Why should I build a table on a MySQL query?
A table built on an SQL query reads data straight from the database, so it suits large or frequently changing data sets better than a file. MySQL tables in wpDataTables can:
- show the result of any
SELECTquery, including joins and conditions, - let MySQL sort, filter and page millions of rows with server-side processing,
- be edited in the admin and on the front end when the query reads one table,
- change with the user or page through placeholders such as
%CURRENT_USER_ID%.
How do I prepare the query?
Write and test the query in your database tool first, so you know it returns the rows you want. To follow along, import the sample employees table:
- Download the sample. Download dummy_employees.sql.
- Import it. In phpMyAdmin, choose the database that wpDataTables uses, open the Import tab, choose the file and click Go.
- Test the query. In the SQL tab, run
SELECT * FROM dummy_employees. It returns the employees with their names, birth date, hire date and position.
Your own query can be as complex as you need, as long as it follows these rules:
How do I create the table from the query?
Create a table linked to an existing data source, choose SQL query as the source type, paste the query and click Save Changes.
- Start a linked table. Go to wpDataTables → Create a Table and click Create a data table linked to an existing data source. If the data is in another database, choose its connection first.
- Name the table. Click New wpDataTable and type a name, for example Staff directory.
- Choose the source type. In the Data source tab, open Input data source type and choose SQL query.
- Paste the query. Paste
SELECT * FROM dummy_employeesin the SQL Query editor. The label above the editor shows the database connection the query runs on, WP Connection for your WordPress database. - Save the table. Click Save Changes. wpDataTables runs the query, creates a column for each field and shows the preview.

Video: creating a table from an SQL query in the table wizard.
If you don’t want to write the query yourself, describe it in AI SQL Assistant next to the editor, for example Create a table of all WP posts that have comments. Leave Mode on Generate, choose the AI model in Model and click Ask AI. Check the query it writes before you click Save Changes.
Tabella AI can’t create a table from an SQL query. If you ask it to, it answers that it can’t run raw SQL or build a table directly from a SELECT query, and points you to the table creation wizard: Create a Table → Create a data table linked to an existing data source, where you paste the query. Afterwards, you can ask Tabella AI in the editor to change the table and column settings.
How do I set up the MySQL table after it is created?
Change the column headers, hide the columns you don’t need and add filters in the editor, the same way as for any other table. For the staff directory, hide the id column and give the other columns readable headers, such as First name and Date hired. You can hide columns quickly in Column List above the preview. Click Save Changes when you’re done, and add the table to a page with its shortcode, as in how to add a table to a page.
What should I remember about MySQL query tables?
Test the query in your database tool, then create a linked table with SQL query as the source type, paste the query and click Save Changes. Keep the query to a SELECT, use a view for variables or nested queries, and turn on server-side processing for large results. The table reads fresh data on every page load.