Okay
  Public Ticket #2274703
SQL Query works in MySQL but not WpDataTables
Closed

Comments

  • Jack Parcell started the conversation

    Hi,

    The following query runs fine in MySQL but I am getting an error when entering it in WpDataTables.

    ---

    SELECT a.ID, r.post_title, a.post_title, a.guid, r.post_type, a.post_type, r.post_author
    FROM (SELECT * FROM `wppm_2_posts` WHERE post_author = %CURRENT_USER_LOGIN% AND post_type = 'reviews' AND post_status = 'publish') as r
    RIGHT JOIN `wppm_2_posts`as a 
    ON r.post_title = a.ID
    WHERE a.post_type = 'applicants'
    AND a.post_status = 'publish'

    ---

    I am getting the following error:

    ERROR: cannot calculate position of as a within ) as r RIGHT JOIN `wppm_2_posts`as a ON r.post_title = a.ID WHERE a.post_type = 'applicants' AND a.post_status = 'publish'


    I've seen a couple other support tickets where the solution is to create a view, however, because I'm pulling the current user's login ID, I don't think that I can use a view.

    Am I missing something or is there a workaround that I can use to accomplish what I need?

    Thanks!

  •  472
    Isidora replied

    Hi Jack, Thank you for your purchase.

    Sorry for late response. 

    We are located in Serbia and our working time is from 10:00 to 17:00 CET. business days.

    Can you try to insert quotes in our placeholder?

    Can you try with this

    SELECT a.ID, r.post_title, a.post_title, a.guid, r.post_type, a.post_type, r.post_author
    FROM (SELECT * FROM `wppm_2_posts` WHERE post_author = '%CURRENT_USER_LOGIN%' AND post_type = 'reviews' AND post_status = 'publish') as r
    RIGHT JOIN `wppm_2_posts`as a 
    ON r.post_title = a.ID
    WHERE a.post_type = 'applicants'
    AND a.post_status = 'publish'
    

    Kind Regards, 

    Isidora Markovic

    wpDataTables: FAQFacebookTwitterFront-end and back-end demoDocs

    Amelia: FAQFacebookTwitter |  Amelia demo sites | Docs

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

    Powerful FiltersGravity Forms Integration for wpDataTablesFormidable Forms Integration for wpDataTables

  • Jack Parcell replied

    Hi Milan,

    Note: I realized I copied the wrong query before. It should have been %CURRENT_USER_ID% instead of %CURRENT_USER_LOGIN% but still getting an error.


    Here is the error I get now:

    ERROR: cannot calculate position of as a within ) as r RIGHT JOIN `wppm_2_posts`as a ON r.post_title = a.ID WHERE a.post_type = 'applicants' AND a.post_status = 'publish'

    Does WpDataTables support subqueries? 

    Attached is the query working in MySQL.


    Thanks,

    Jack

  •  2,572
    Aleksandar replied

    Hello Jack

    I'll take over Milan's ticket.

    It should work like that. We've had users work with LEFT, RIGHT and INNER JOINs without much issues. That is - the table generated correctly, but search, filtering and sorting may misbehave.

    This is probably happening because wpDataTables server has problems with parsing of the query and building new queries dynamically (rarely happens, but does sometimes). To avoid this please prepare a MySQL view (a stored query), which will return the data that you need, call it e.g. “view1” and then build a wpDataTabled based on a simple query like “SELECT * FROM view1″.

    Please note some this when working with the server-side processing feature:

    • Please do not use “LIMIT” in the SELECT statement. wpDataTables adds it automatically and it will be overridden.
    • Please do not use “ORDER BY” in the SELECT statement. wpDataTables has its own sorting engine so it makes no sense to use MySQL’s sorting, since it will be overridden. Also, server-side processing feature adds this part of statement automatically when users trigger the sorting on the front-end, and having it in initial statement may cause the table to crash.

    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

  • Jack Parcell replied

    The table should be generated correctly as it works in MySQL. As mentioned before, I have a dynamic placeholder and a subquery... both of which wouldn't work with creating a view.

    Any idea what the error is saying is the issue?

    I am also not suing LIMIT or ORDER BY.

  • Jack Parcell replied

    Here is the correct way of writing the query in case anyone finds themselves in a similar situation. This query works with WpDataTables


    SELECT 
    APPLICANTS.ID AS applicants_id
        ,APPLICANTS.post_title AS applicants_post_title
    ,APPLICANTS.guid AS applicants_guid
        ,APPLICANTS.post_type AS applicants_post_type
        ,REVIEWS.post_author AS reviews_author_id
        ,REVIEWS.post_type AS reviews_post_type
        ,REVIEWS.post_title AS reviews_post_title
    ,R_APP_SCORES.meta_value AS review_app_score
    ,R_APP_COMMENTS.meta_value AS review_app_comment
    FROM 
    `%WPDB%posts` AS APPLICANTS 
        LEFT JOIN
        `%WPDB%posts` AS REVIEWS ON
      (REVIEWS.post_author = %CURRENT_USER_ID%) AND     
    (REVIEWS.post_title = APPLICANTS.ID) AND 
            (REVIEWS.post_type = 'reviews') AND 
            (REVIEWS.post_status = 'publish')
    LEFT JOIN 
        `%WPDB%postmeta` R_APP_SCORES ON
    (R_APP_SCORES.post_id = REVIEWS.ID) AND 
            (R_APP_SCORES.meta_key = 'review_app_score')
    LEFT JOIN 
        `%WPDB%postmeta` R_APP_COMMENTS ON 
    (R_APP_COMMENTS.post_id = REVIEWS.ID) AND
    (R_APP_COMMENTS.meta_key = 'review_app_comment')
    WHERE 
    (APPLICANTS.post_type = 'applicants') AND
    (APPLICANTS.post_status = 'publish') 


  •  2,572
    Aleksandar replied

    Thank you for sharing the query with everyone, Jack.

    If you have any further questions or issues, please feel free to open a new ticket, and we'll gladly help.

    Best regards.

    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