Table of Contents
How to build a MySQL query with the wpDataTables query constructor
Say you want a table of all published blog posts with the name of the author next to each title, but the titles live in one database table and the author names in another, and you’ve never written a SQL join. The MySQL query constructor writes the query for you: you pick the tables and columns, say how the tables connect, add a condition or two, and wpDataTables shows the generated query with a preview before it creates the table.
What does the MySQL query constructor do?
The constructor builds a SELECT query from the tables, columns, joins, conditions and grouping you choose, and creates a table based on that query. It works with any table in the database that wpDataTables uses, or in a separate database connection. For WordPress posts, pages and custom fields, the WordPress database query constructor is easier, because it already knows how posts and meta fields connect.
How do I choose the tables and columns?
Go to wpDataTables → Create a Table, click Generate a query to the MySQL database, and move the tables and then the columns you need into the selected lists.
- Start the constructor. Go to wpDataTables → Create a Table. If the data is in another database, choose it in Connection. Click Generate a query to the MySQL database.
- Choose the tables. In All SQL tables, select the tables you need, for example
wp_postsandwp_users, and move them to Selected SQL tables with the arrow button. Select All selects every table in a list. - Choose the columns. All SQL columns now lists the columns of the selected tables. Move the ones you want in the table, for example
wp_posts.post_date,wp_posts.post_titleandwp_users.display_name, to Selected SQL columns. - Join the tables. When you select more than one table, Define SQL tables relations shows a row for each. Pick the column of one table and the matching column of the other, for example
wp_posts.post_author=wp_users.ID, and turn on the switch at the end of the row for an inner join. Leave the row of the other table on Nothing selected.

Video: building a MySQL query with the query constructor.
You’ll usually need one relation block less than the number of tables, for example one join for two tables.
How do I add conditions and grouping?
Add a condition to return only some rows, and a grouping rule to show each row once. Both are optional:
- Conditions. Under Add conditions, click + Add condition, choose a column and an operator, and enter the value, for example
wp_posts.post_status=publish. Add as many conditions as you need, and remove them with the trash icon. - Grouping. Under Add grouping rules, click + Add grouping and choose a column. Use it when a join returns the same item several times, for example a post once per tag. Grouped by the post title, each post shows once with its values separated by commas.
These operators are available in conditions:
| Operator | Returns rows where the column |
|---|---|
=, <> |
equals, or doesn’t equal, the value |
<, <=, >, >= |
is less than, less than or equal to, greater than, or greater than or equal to the value |
LIKE |
matches a pattern, where % stands for any characters and _ for one character, for example abc% |
%LIKE% |
contains the value anywhere |
IN |
is one of a list of values, for example 1,2,3 |
How do I check the query and create the table?
Click Next, check the generated query and the preview, adjust the query if needed, and click Create the table.
- Open the query. Click Next. The constructor shows the query in Preview the query that has been generated for you, and its first rows in Preview the 5 first result rows.
- Adjust the query. Edit the query if needed, for example add column aliases such as
AS Authorwhen two columns have the same name. Click the refresh icon above the editor to update the preview. - Create the table. Click Create the table. wpDataTables creates a table based on the query and opens it in the editor.

If the preview shows “No data”, the query returns no rows, either because of an error or because no rows match. Check the joins and conditions. After the table is created, it works like any table built on an SQL query: change the headers and filters in the editor, and use placeholders in the query to make it dynamic.
What should I remember about the MySQL query constructor?
Click Generate a query to the MySQL database in the wizard, choose the tables and columns, join the tables with one relation rule less than the number of tables, and add conditions or grouping if needed. Check the query and preview after Next, adjust the query by hand if it doesn’t return what you need, and click Create the table.
Where can I read more?
These pages cover other ways to build tables from a database: