Join query with alias name





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-1















I have a query to search for three things, a keyword, a category and a region (it's for vacancies). Everything works except for the region since I get that column from my database with an aliased name.



My code now:



$functie = $_POST['functie'];
$branche = $_POST['branche'];
$regio = $_POST['regio'];

$search = "
SELECT cnt.title as content_title, cnt.alias as content_alias, cnt.images, cnt.introtext, cnt.catid, cat.title as cat_title, cat.alias as cat_alias,
MAX(case when f.field_id = 2 then f.value end) as regio
FROM snm_content cnt
LEFT JOIN snm_categories cat
ON cat.id = cnt.catid
LEFT JOIN snm_fields_values f
ON cnt.id = f.item_id
";
// Collect all the where conditions in an array
$whr = array();

// check if $functie has some value in input filter
if (!empty($functie)) {
$whr = "cnt.title LIKE '%" . $functie . "%'";
}

if (!empty($regio)) {
$whr = "f.value LIKE '%" . $regio . "%'";
}

// check if $branche has some value in input filter
if (!empty($branche)) {
$whr = "cat.title LIKE '%" . $branche . "%'";
}

$where_sql = '';
// Prepare where part of the SQL
if (!empty($whr)) {

$where_sql = ' WHERE ' . implode(' OR ', $whr);
}

// Append to the original sql
$search .= $where_sql;
$searchcon = $conn->query($search);


But above code is not working for the region.



However this is the endresult query and these are the results I get with it:



SELECT cnt.title as content_title, cnt.alias as content_alias, cnt.images, cnt.introtext, cnt.catid, cat.title as cat_title, cat.alias as cat_alias,
MAX(case when f.field_id = 2 then f.value end) as regio
FROM snm_content cnt
LEFT JOIN snm_categories cat
ON cat.id = cnt.catid
LEFT JOIN snm_fields_values f
ON cnt.id = f.item_id
WHERE cnt.title LIKE '%vrachtwagen%'


enter image description here



regio is what I want to show results for.



But WHERE regio LIKE '%" . $regio . "%'"; is not working, it says unknown column regio because it is an alias. How can I only show all results belonging to the region that was posted? For example above image, show only that row when Drenthe is the posted region.










share|improve this question

























  • which version of mysql db you are using ??

    – scaisEdge
    Nov 23 '18 at 13:47













  • @scaisEdge mysql phpmyadmin

    – twan
    Nov 23 '18 at 13:48











  • whic version of db not which ide ..

    – scaisEdge
    Nov 23 '18 at 13:48













  • @twan Please elaborate further the logic for determining regio; maybe there is a different way around.

    – Madhur Bhaiya
    Nov 23 '18 at 13:53


















-1















I have a query to search for three things, a keyword, a category and a region (it's for vacancies). Everything works except for the region since I get that column from my database with an aliased name.



My code now:



$functie = $_POST['functie'];
$branche = $_POST['branche'];
$regio = $_POST['regio'];

$search = "
SELECT cnt.title as content_title, cnt.alias as content_alias, cnt.images, cnt.introtext, cnt.catid, cat.title as cat_title, cat.alias as cat_alias,
MAX(case when f.field_id = 2 then f.value end) as regio
FROM snm_content cnt
LEFT JOIN snm_categories cat
ON cat.id = cnt.catid
LEFT JOIN snm_fields_values f
ON cnt.id = f.item_id
";
// Collect all the where conditions in an array
$whr = array();

// check if $functie has some value in input filter
if (!empty($functie)) {
$whr = "cnt.title LIKE '%" . $functie . "%'";
}

if (!empty($regio)) {
$whr = "f.value LIKE '%" . $regio . "%'";
}

// check if $branche has some value in input filter
if (!empty($branche)) {
$whr = "cat.title LIKE '%" . $branche . "%'";
}

$where_sql = '';
// Prepare where part of the SQL
if (!empty($whr)) {

$where_sql = ' WHERE ' . implode(' OR ', $whr);
}

// Append to the original sql
$search .= $where_sql;
$searchcon = $conn->query($search);


But above code is not working for the region.



However this is the endresult query and these are the results I get with it:



SELECT cnt.title as content_title, cnt.alias as content_alias, cnt.images, cnt.introtext, cnt.catid, cat.title as cat_title, cat.alias as cat_alias,
MAX(case when f.field_id = 2 then f.value end) as regio
FROM snm_content cnt
LEFT JOIN snm_categories cat
ON cat.id = cnt.catid
LEFT JOIN snm_fields_values f
ON cnt.id = f.item_id
WHERE cnt.title LIKE '%vrachtwagen%'


enter image description here



regio is what I want to show results for.



But WHERE regio LIKE '%" . $regio . "%'"; is not working, it says unknown column regio because it is an alias. How can I only show all results belonging to the region that was posted? For example above image, show only that row when Drenthe is the posted region.










share|improve this question

























  • which version of mysql db you are using ??

    – scaisEdge
    Nov 23 '18 at 13:47













  • @scaisEdge mysql phpmyadmin

    – twan
    Nov 23 '18 at 13:48











  • whic version of db not which ide ..

    – scaisEdge
    Nov 23 '18 at 13:48













  • @twan Please elaborate further the logic for determining regio; maybe there is a different way around.

    – Madhur Bhaiya
    Nov 23 '18 at 13:53














-1












-1








-1








I have a query to search for three things, a keyword, a category and a region (it's for vacancies). Everything works except for the region since I get that column from my database with an aliased name.



My code now:



$functie = $_POST['functie'];
$branche = $_POST['branche'];
$regio = $_POST['regio'];

$search = "
SELECT cnt.title as content_title, cnt.alias as content_alias, cnt.images, cnt.introtext, cnt.catid, cat.title as cat_title, cat.alias as cat_alias,
MAX(case when f.field_id = 2 then f.value end) as regio
FROM snm_content cnt
LEFT JOIN snm_categories cat
ON cat.id = cnt.catid
LEFT JOIN snm_fields_values f
ON cnt.id = f.item_id
";
// Collect all the where conditions in an array
$whr = array();

// check if $functie has some value in input filter
if (!empty($functie)) {
$whr = "cnt.title LIKE '%" . $functie . "%'";
}

if (!empty($regio)) {
$whr = "f.value LIKE '%" . $regio . "%'";
}

// check if $branche has some value in input filter
if (!empty($branche)) {
$whr = "cat.title LIKE '%" . $branche . "%'";
}

$where_sql = '';
// Prepare where part of the SQL
if (!empty($whr)) {

$where_sql = ' WHERE ' . implode(' OR ', $whr);
}

// Append to the original sql
$search .= $where_sql;
$searchcon = $conn->query($search);


But above code is not working for the region.



However this is the endresult query and these are the results I get with it:



SELECT cnt.title as content_title, cnt.alias as content_alias, cnt.images, cnt.introtext, cnt.catid, cat.title as cat_title, cat.alias as cat_alias,
MAX(case when f.field_id = 2 then f.value end) as regio
FROM snm_content cnt
LEFT JOIN snm_categories cat
ON cat.id = cnt.catid
LEFT JOIN snm_fields_values f
ON cnt.id = f.item_id
WHERE cnt.title LIKE '%vrachtwagen%'


enter image description here



regio is what I want to show results for.



But WHERE regio LIKE '%" . $regio . "%'"; is not working, it says unknown column regio because it is an alias. How can I only show all results belonging to the region that was posted? For example above image, show only that row when Drenthe is the posted region.










share|improve this question
















I have a query to search for three things, a keyword, a category and a region (it's for vacancies). Everything works except for the region since I get that column from my database with an aliased name.



My code now:



$functie = $_POST['functie'];
$branche = $_POST['branche'];
$regio = $_POST['regio'];

$search = "
SELECT cnt.title as content_title, cnt.alias as content_alias, cnt.images, cnt.introtext, cnt.catid, cat.title as cat_title, cat.alias as cat_alias,
MAX(case when f.field_id = 2 then f.value end) as regio
FROM snm_content cnt
LEFT JOIN snm_categories cat
ON cat.id = cnt.catid
LEFT JOIN snm_fields_values f
ON cnt.id = f.item_id
";
// Collect all the where conditions in an array
$whr = array();

// check if $functie has some value in input filter
if (!empty($functie)) {
$whr = "cnt.title LIKE '%" . $functie . "%'";
}

if (!empty($regio)) {
$whr = "f.value LIKE '%" . $regio . "%'";
}

// check if $branche has some value in input filter
if (!empty($branche)) {
$whr = "cat.title LIKE '%" . $branche . "%'";
}

$where_sql = '';
// Prepare where part of the SQL
if (!empty($whr)) {

$where_sql = ' WHERE ' . implode(' OR ', $whr);
}

// Append to the original sql
$search .= $where_sql;
$searchcon = $conn->query($search);


But above code is not working for the region.



However this is the endresult query and these are the results I get with it:



SELECT cnt.title as content_title, cnt.alias as content_alias, cnt.images, cnt.introtext, cnt.catid, cat.title as cat_title, cat.alias as cat_alias,
MAX(case when f.field_id = 2 then f.value end) as regio
FROM snm_content cnt
LEFT JOIN snm_categories cat
ON cat.id = cnt.catid
LEFT JOIN snm_fields_values f
ON cnt.id = f.item_id
WHERE cnt.title LIKE '%vrachtwagen%'


enter image description here



regio is what I want to show results for.



But WHERE regio LIKE '%" . $regio . "%'"; is not working, it says unknown column regio because it is an alias. How can I only show all results belonging to the region that was posted? For example above image, show only that row when Drenthe is the posted region.







php mysql sql database






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 13:47









Madhur Bhaiya

19.6k62336




19.6k62336










asked Nov 23 '18 at 13:40









twantwan

98321437




98321437













  • which version of mysql db you are using ??

    – scaisEdge
    Nov 23 '18 at 13:47













  • @scaisEdge mysql phpmyadmin

    – twan
    Nov 23 '18 at 13:48











  • whic version of db not which ide ..

    – scaisEdge
    Nov 23 '18 at 13:48













  • @twan Please elaborate further the logic for determining regio; maybe there is a different way around.

    – Madhur Bhaiya
    Nov 23 '18 at 13:53



















  • which version of mysql db you are using ??

    – scaisEdge
    Nov 23 '18 at 13:47













  • @scaisEdge mysql phpmyadmin

    – twan
    Nov 23 '18 at 13:48











  • whic version of db not which ide ..

    – scaisEdge
    Nov 23 '18 at 13:48













  • @twan Please elaborate further the logic for determining regio; maybe there is a different way around.

    – Madhur Bhaiya
    Nov 23 '18 at 13:53

















which version of mysql db you are using ??

– scaisEdge
Nov 23 '18 at 13:47







which version of mysql db you are using ??

– scaisEdge
Nov 23 '18 at 13:47















@scaisEdge mysql phpmyadmin

– twan
Nov 23 '18 at 13:48





@scaisEdge mysql phpmyadmin

– twan
Nov 23 '18 at 13:48













whic version of db not which ide ..

– scaisEdge
Nov 23 '18 at 13:48







whic version of db not which ide ..

– scaisEdge
Nov 23 '18 at 13:48















@twan Please elaborate further the logic for determining regio; maybe there is a different way around.

– Madhur Bhaiya
Nov 23 '18 at 13:53





@twan Please elaborate further the logic for determining regio; maybe there is a different way around.

– Madhur Bhaiya
Nov 23 '18 at 13:53












2 Answers
2






active

oldest

votes


















0














We cannot use an aliased calculation expression in the Select clause inside the Where clause. Remember that Select is evaluated after Where, so expression evaluation happens afterwards.



You will have to specify the complete expression again in the Where clause.



// check if $regio has some value in input filter
if (!empty($regio)) {
$whr = "MAX(case when f.field_id = 2 then f.value end) LIKE '%" . $regio . "%'";
}





share|improve this answer
























  • I get: There was an error running the query [Invalid use of group function]

    – twan
    Nov 23 '18 at 13:47











  • @twan why do you use Max() function without Group By ?

    – Madhur Bhaiya
    Nov 23 '18 at 13:48



















0














looking to your code seems you are trying to filter ana ggregated result max(...) as regio



for filter an aggregated result you should use having



HAVING regio  like concat('%', $your_value, '%')


anyway you are using php var inside sql this your code is at risk for sqlinjecttion .. you should take a look at your db driver for prepared statement and binding param



You are also using not aggregated column in select without group by .. this is deprecated in sql, the result for the columns in unpredictable and in most recent version of mysql in not allowed.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447799%2fjoin-query-with-alias-name%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    We cannot use an aliased calculation expression in the Select clause inside the Where clause. Remember that Select is evaluated after Where, so expression evaluation happens afterwards.



    You will have to specify the complete expression again in the Where clause.



    // check if $regio has some value in input filter
    if (!empty($regio)) {
    $whr = "MAX(case when f.field_id = 2 then f.value end) LIKE '%" . $regio . "%'";
    }





    share|improve this answer
























    • I get: There was an error running the query [Invalid use of group function]

      – twan
      Nov 23 '18 at 13:47











    • @twan why do you use Max() function without Group By ?

      – Madhur Bhaiya
      Nov 23 '18 at 13:48
















    0














    We cannot use an aliased calculation expression in the Select clause inside the Where clause. Remember that Select is evaluated after Where, so expression evaluation happens afterwards.



    You will have to specify the complete expression again in the Where clause.



    // check if $regio has some value in input filter
    if (!empty($regio)) {
    $whr = "MAX(case when f.field_id = 2 then f.value end) LIKE '%" . $regio . "%'";
    }





    share|improve this answer
























    • I get: There was an error running the query [Invalid use of group function]

      – twan
      Nov 23 '18 at 13:47











    • @twan why do you use Max() function without Group By ?

      – Madhur Bhaiya
      Nov 23 '18 at 13:48














    0












    0








    0







    We cannot use an aliased calculation expression in the Select clause inside the Where clause. Remember that Select is evaluated after Where, so expression evaluation happens afterwards.



    You will have to specify the complete expression again in the Where clause.



    // check if $regio has some value in input filter
    if (!empty($regio)) {
    $whr = "MAX(case when f.field_id = 2 then f.value end) LIKE '%" . $regio . "%'";
    }





    share|improve this answer













    We cannot use an aliased calculation expression in the Select clause inside the Where clause. Remember that Select is evaluated after Where, so expression evaluation happens afterwards.



    You will have to specify the complete expression again in the Where clause.



    // check if $regio has some value in input filter
    if (!empty($regio)) {
    $whr = "MAX(case when f.field_id = 2 then f.value end) LIKE '%" . $regio . "%'";
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 23 '18 at 13:46









    Madhur BhaiyaMadhur Bhaiya

    19.6k62336




    19.6k62336













    • I get: There was an error running the query [Invalid use of group function]

      – twan
      Nov 23 '18 at 13:47











    • @twan why do you use Max() function without Group By ?

      – Madhur Bhaiya
      Nov 23 '18 at 13:48



















    • I get: There was an error running the query [Invalid use of group function]

      – twan
      Nov 23 '18 at 13:47











    • @twan why do you use Max() function without Group By ?

      – Madhur Bhaiya
      Nov 23 '18 at 13:48

















    I get: There was an error running the query [Invalid use of group function]

    – twan
    Nov 23 '18 at 13:47





    I get: There was an error running the query [Invalid use of group function]

    – twan
    Nov 23 '18 at 13:47













    @twan why do you use Max() function without Group By ?

    – Madhur Bhaiya
    Nov 23 '18 at 13:48





    @twan why do you use Max() function without Group By ?

    – Madhur Bhaiya
    Nov 23 '18 at 13:48













    0














    looking to your code seems you are trying to filter ana ggregated result max(...) as regio



    for filter an aggregated result you should use having



    HAVING regio  like concat('%', $your_value, '%')


    anyway you are using php var inside sql this your code is at risk for sqlinjecttion .. you should take a look at your db driver for prepared statement and binding param



    You are also using not aggregated column in select without group by .. this is deprecated in sql, the result for the columns in unpredictable and in most recent version of mysql in not allowed.






    share|improve this answer




























      0














      looking to your code seems you are trying to filter ana ggregated result max(...) as regio



      for filter an aggregated result you should use having



      HAVING regio  like concat('%', $your_value, '%')


      anyway you are using php var inside sql this your code is at risk for sqlinjecttion .. you should take a look at your db driver for prepared statement and binding param



      You are also using not aggregated column in select without group by .. this is deprecated in sql, the result for the columns in unpredictable and in most recent version of mysql in not allowed.






      share|improve this answer


























        0












        0








        0







        looking to your code seems you are trying to filter ana ggregated result max(...) as regio



        for filter an aggregated result you should use having



        HAVING regio  like concat('%', $your_value, '%')


        anyway you are using php var inside sql this your code is at risk for sqlinjecttion .. you should take a look at your db driver for prepared statement and binding param



        You are also using not aggregated column in select without group by .. this is deprecated in sql, the result for the columns in unpredictable and in most recent version of mysql in not allowed.






        share|improve this answer













        looking to your code seems you are trying to filter ana ggregated result max(...) as regio



        for filter an aggregated result you should use having



        HAVING regio  like concat('%', $your_value, '%')


        anyway you are using php var inside sql this your code is at risk for sqlinjecttion .. you should take a look at your db driver for prepared statement and binding param



        You are also using not aggregated column in select without group by .. this is deprecated in sql, the result for the columns in unpredictable and in most recent version of mysql in not allowed.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 13:49









        scaisEdgescaisEdge

        97.2k105272




        97.2k105272






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447799%2fjoin-query-with-alias-name%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini