Our Biggest Sale Is Here
Up to 70% Off
Get the Deal

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:

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:

  1. Download the sample. Download dummy_employees.sql.
  2. Import it. In phpMyAdmin, choose the database that wpDataTables uses, open the Import tab, choose the file and click Go.
  3. 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:

  • Use a SELECT query. DROP, DELETE and similar statements are blocked for security.
  • Variables (@), stored procedures and nested queries aren’t supported. Save the logic in a MySQL view, for example view1, and use SELECT * FROM view1.
  • You don’t need backticks around table names. wpDataTables adds them for MySQL.
  • With server-side processing, leave out LIMIT and ORDER BY, as explained in what to leave out of the query.

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.

  1. 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.
  2. Name the table. Click New wpDataTable and type a name, for example Staff directory.
  3. Choose the source type. In the Data source tab, open Input data source type and choose SQL query.
  4. Paste the query. Paste SELECT * FROM dummy_employees in the SQL Query editor. The label above the editor shows the database connection the query runs on, WP Connection for your WordPress database.
  5. Save the table. Click Save Changes. wpDataTables runs the query, creates a column for each field and shows the preview.
The Data source tab of a new table with SQL query as the input data source type, a SELECT query in the SQL Query editor, Server-side processing on and the AI SQL Assistant
Linking a table to an SQL query: (1) Input data source type, (2) the SQL Query editor, (3) Server-side processing, (4) AI SQL Assistant, (5) Save Changes.

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.

Above 2,000 to 3,000 rows, turn on Enable server-side processing under Server-side processing in the Data source tab. SQL tables with more than 2,000 rows use it automatically.

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.

Wait, Before You Download!

To get your hands on wpDataTables Lite, please enter your email address below. We’ll send you a direct download link and keep you updated on existing features along with helpful tips and tricks!

By continuing, I accept Privacy Policy and T&C
Mail Box