Hey everyone!

With the holiday season upon us, we want to share our upcoming working hours:

After that, we’ll return to our regular schedule and assist you as quickly as possible.

In the meantime, you can explore our documentation for Amelia and wpDataTables. You'll find tons of helpful resources, including articles and handy video tutorials on YouTube (Amelia's YouTube Channel and wpDataTables' YouTube Channel), which might just have the answers you need while we’re away.

Thanks a bunch for your understanding and support!

Warm regards and happy holidays!

TMS

Okay
  Public Ticket #3394197
Filter query SQL wrong if using table join
Closed

Comments

  • David Chalmers started the conversation

    Hi there,

    When I set up a wpDataTable using a table join: filters on the columns which live in the 'first' table in the JOIN work fine, but filters on any column in the 'second' table throw an error in the WordPress debug.log.

    E.g. the wpDataTable is based on this SELECT query on two tables, Awards and Projects:
     
            SELECT Projects.Pi, Projects.RGA, Projects.ProjectNo,
                          Award.RGNo, Award.AwardDep, Award.StartDate
            FROM Projects  
            INNER JOIN Award ON Award.RGNo = Projects.RGNo

    If I add a filter on the AwardDep column in the 'second' table in the JOIN, I get:

            Unknown column 'Projects.AwardDep' in 'where clause' for query

            SELECT SQL_CALC_FOUND_ROWS Projects.Pi, Award.RGNo, Award.AwardDep
             FROM Projects
            INNER JOIN Award ON Award.RGNo = Projects.RGNo         WHERE `Projects`.`AwardDep` LIKE '%oncology%'
                   ^^^^^^^^

    ...When constructing the filter query, clearly wpDataTables is trying to use the 'first' table named in the join, and the column doesn't exist in that table. If it used the fully-qualified named column (Award.AwardDep) in the WHERE clause, as it does in the SELECT clause, it would be OK.

    I see in other tickets that you recommend using a View for filtering JOINed tables. I will try that, but I have upwards of 15 wpDataTables defined on JOINed tables, and each of them would need me to create a MySQL View first. So it would be great if the plugin could handle this case :)

    I must also say the wpDataTables plugin has been very useful, and saved a lot of time on my current project; thank you!

    Best wishes,
    David C
    Software Team Leader
    Cancer Research UK Cambridge Institute

  •  1,847
    Miloš replied


    Hi, David.

    Firstly, I would like to sincerely apologize for the delayed response as we have been experiencing an unusually high number of tickets. I am sorry that it has taken longer than usual to respond to your concern and your patience is highly appreciated.

    -

    You are right - our SQL Parser has quite a few limitations,

    when you enable server-side processing in SQL Tables in our Plugin.

    I will do my best to advise on most important details about this.


    Our logic is based on a PHP SQL parser which has full support for the SQL dialect for the following statement types

    SELECT, INSERT, UPDATE, DELETE, REPLACE, RENAME, SHOW, SET, DROP, CREATE INDEX, CREATE TABLE, EXPLAIN and DESCRIBE.

    Some of them are disabled for security reasons.

    Filtering, sorting, and search may not work properly if you include:

    • Accent graves ( ` ) around the table name
    • JOIN functions
    • UNION functions
    • CONCAT functions
    • sub-queries

    -

    So, first you can check for accent graves around the table name, if you have this, remove it...

    Then, see if you used CONCAT to create any column.

    If so, go into this column setting, and disable it from "global search" in the Filtering tab.

    -

    7138515403.png

    -

    If none of that helps,

    you can try preparing a MySQL view (which will return the data that you need, call it e.g. “view1” and then build a wpDataTables based on a simple query like "SELECT * FROM view1″.

     If you need help with that, you can see our video, where we show an example of using View in our plugin.

    I hope that helps.

    Our developers will do their best to try and improve this in the future,

    but i can't say a realistic ETA on it.

    If you have an idea how they could improve it,

    Please feel free to search on our suggestions page

     to see if someone may be already suggested this feature. If you can't see it, feel free to add your suggestion there,  and as more people vote, the feature will move higher on the priority list.

    You can certainly follow our changeLog page if you'd like ( it is also available in the plugin dashboard), where we state any changes/new features/bug fixes during updates;

    and our newsletter, so you're informed about new features, bug fixes, freebies, etc.


    Kind Regards, 

    Miloš Jovanović
    [email protected]

    Rate my support

    Try our FREE mapping plugin! MapSVG - easy Google maps, interactive SVG maps and floor plans, choropleth maps and much more - https://wordpress.org/plugins/mapsvg-lite-interactive-vector-maps/

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables

  • David Chalmers replied

    Hi Milos,

    Thanks for your reply. I think this is actually a bug, and it would be great if the developers could take a look.

    As I said, if the SQL filter query used the fully-qualified named column (Award.AwardDep) in the WHERE clause, as it does in the SELECT clause, it would be OK.  It is the inconsistency between the SELECT clause and the WHERE clause that is the issue.

    I will most likely create a VIEW as a work-around in this case, but it's going to be a pain to go down that road for the 20+ wpDataTables I now have.

    Anyway, all the best.

    David

  •  2,572
    Aleksandar replied

    Hey David.

    I would also advise creating a view since, as Milos mentioned, we're using a 3rd party PHP-SQL parser which is limited.

    Our developers are trying to improve it with every update, but with it being a 3rd party parser, it's not easy doing that. Maybe in the future we'll include a better one, or we'll develop our own. I can't say anything at this time.

    Kind Regards, 

    Aleksandar Vuković
    [email protected]

    Rate my support

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables