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

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.

The constructor suggests a query based on your choices. The more complex the request, the more likely you’ll need to adjust the generated query by hand before you create the table.

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.

  1. 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.
  2. Choose the tables. In All SQL tables, select the tables you need, for example wp_posts and wp_users, and move them to Selected SQL tables with the arrow button. Select All selects every table in a list.
  3. 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_title and wp_users.display_name, to Selected SQL columns.
  4. 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.
The MySQL query constructor with wp_posts and wp_users in Selected SQL tables, three Selected SQL columns, a table relation and a post status condition
Building a MySQL query, shown as overlapping panels: (1) Selected SQL tables, (2) Selected SQL columns, (3) the table relation with the inner join switch, (4) a condition.

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.

  1. 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.
  2. Adjust the query. Edit the query if needed, for example add column aliases such as AS Author when two columns have the same name. Click the refresh icon above the editor to update the preview.
  3. Create the table. Click Create the table. wpDataTables creates a table based on the query and opens it in the editor.
The generated MySQL query with an INNER JOIN of wp_posts and wp_users in the query editor and the preview of the first five result rows
The generated query, shown as overlapping panels: (1) the query, (2) refresh the query and preview, (3) the first five result rows, (4) Create the table.

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.

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