Mysql LEFT JOIN with count returns Unknown column











up vote
0
down vote

favorite












dear i have below query to count how many spams for each user and the total orders



i did the left join because not all orders have a spam



select users.firstName,users.lastName,users.Id,users.phoneNumber,count(CASE 
WHEN comments.`commentType` = "spam" THEN 1 ELSE NULL END) as countSpam,
count(`orders`.`id`) as totalOrder
from `orders`,users,providers
LEFT JOIN comments ON `orders`.`id`= `comments`.`commentableId`
where
`orders`.`providerId` = `providers`.id
and
users.id = `providers`.userId
and
`orders`.`createdAt` >= (CURDATE() - INTERVAL 7 DAY)
GROUP BY users.id
ORDER BY countSpam DESC;


am getting the below error from mysql




Unknown column 'orders.id' in 'on clause'




What is the issue here ? i did the LEFT JOIN correct based on old query working fine










share|improve this question






















  • Looks like orders doesn't have an id column. Recommend using JOIN against users and providers too.
    – danblack
    Nov 21 at 0:16










  • it's existing dear because it's the primary key :D i double checked before
    – Faisal
    Nov 21 at 0:36










  • Please don't use Old comma based Implicit joins and use Modern Explicit Join based syntax. Switch to ON clause based code; you shall be able to see the mistake automatically :-) Also, in such multi-table queries, it is generally better to use Aliasing.
    – Madhur Bhaiya
    2 days ago















up vote
0
down vote

favorite












dear i have below query to count how many spams for each user and the total orders



i did the left join because not all orders have a spam



select users.firstName,users.lastName,users.Id,users.phoneNumber,count(CASE 
WHEN comments.`commentType` = "spam" THEN 1 ELSE NULL END) as countSpam,
count(`orders`.`id`) as totalOrder
from `orders`,users,providers
LEFT JOIN comments ON `orders`.`id`= `comments`.`commentableId`
where
`orders`.`providerId` = `providers`.id
and
users.id = `providers`.userId
and
`orders`.`createdAt` >= (CURDATE() - INTERVAL 7 DAY)
GROUP BY users.id
ORDER BY countSpam DESC;


am getting the below error from mysql




Unknown column 'orders.id' in 'on clause'




What is the issue here ? i did the LEFT JOIN correct based on old query working fine










share|improve this question






















  • Looks like orders doesn't have an id column. Recommend using JOIN against users and providers too.
    – danblack
    Nov 21 at 0:16










  • it's existing dear because it's the primary key :D i double checked before
    – Faisal
    Nov 21 at 0:36










  • Please don't use Old comma based Implicit joins and use Modern Explicit Join based syntax. Switch to ON clause based code; you shall be able to see the mistake automatically :-) Also, in such multi-table queries, it is generally better to use Aliasing.
    – Madhur Bhaiya
    2 days ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











dear i have below query to count how many spams for each user and the total orders



i did the left join because not all orders have a spam



select users.firstName,users.lastName,users.Id,users.phoneNumber,count(CASE 
WHEN comments.`commentType` = "spam" THEN 1 ELSE NULL END) as countSpam,
count(`orders`.`id`) as totalOrder
from `orders`,users,providers
LEFT JOIN comments ON `orders`.`id`= `comments`.`commentableId`
where
`orders`.`providerId` = `providers`.id
and
users.id = `providers`.userId
and
`orders`.`createdAt` >= (CURDATE() - INTERVAL 7 DAY)
GROUP BY users.id
ORDER BY countSpam DESC;


am getting the below error from mysql




Unknown column 'orders.id' in 'on clause'




What is the issue here ? i did the LEFT JOIN correct based on old query working fine










share|improve this question













dear i have below query to count how many spams for each user and the total orders



i did the left join because not all orders have a spam



select users.firstName,users.lastName,users.Id,users.phoneNumber,count(CASE 
WHEN comments.`commentType` = "spam" THEN 1 ELSE NULL END) as countSpam,
count(`orders`.`id`) as totalOrder
from `orders`,users,providers
LEFT JOIN comments ON `orders`.`id`= `comments`.`commentableId`
where
`orders`.`providerId` = `providers`.id
and
users.id = `providers`.userId
and
`orders`.`createdAt` >= (CURDATE() - INTERVAL 7 DAY)
GROUP BY users.id
ORDER BY countSpam DESC;


am getting the below error from mysql




Unknown column 'orders.id' in 'on clause'




What is the issue here ? i did the LEFT JOIN correct based on old query working fine







jquery mysql database






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 0:05









Faisal

599




599












  • Looks like orders doesn't have an id column. Recommend using JOIN against users and providers too.
    – danblack
    Nov 21 at 0:16










  • it's existing dear because it's the primary key :D i double checked before
    – Faisal
    Nov 21 at 0:36










  • Please don't use Old comma based Implicit joins and use Modern Explicit Join based syntax. Switch to ON clause based code; you shall be able to see the mistake automatically :-) Also, in such multi-table queries, it is generally better to use Aliasing.
    – Madhur Bhaiya
    2 days ago


















  • Looks like orders doesn't have an id column. Recommend using JOIN against users and providers too.
    – danblack
    Nov 21 at 0:16










  • it's existing dear because it's the primary key :D i double checked before
    – Faisal
    Nov 21 at 0:36










  • Please don't use Old comma based Implicit joins and use Modern Explicit Join based syntax. Switch to ON clause based code; you shall be able to see the mistake automatically :-) Also, in such multi-table queries, it is generally better to use Aliasing.
    – Madhur Bhaiya
    2 days ago
















Looks like orders doesn't have an id column. Recommend using JOIN against users and providers too.
– danblack
Nov 21 at 0:16




Looks like orders doesn't have an id column. Recommend using JOIN against users and providers too.
– danblack
Nov 21 at 0:16












it's existing dear because it's the primary key :D i double checked before
– Faisal
Nov 21 at 0:36




it's existing dear because it's the primary key :D i double checked before
– Faisal
Nov 21 at 0:36












Please don't use Old comma based Implicit joins and use Modern Explicit Join based syntax. Switch to ON clause based code; you shall be able to see the mistake automatically :-) Also, in such multi-table queries, it is generally better to use Aliasing.
– Madhur Bhaiya
2 days ago




Please don't use Old comma based Implicit joins and use Modern Explicit Join based syntax. Switch to ON clause based code; you shall be able to see the mistake automatically :-) Also, in such multi-table queries, it is generally better to use Aliasing.
– Madhur Bhaiya
2 days ago












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










i fixed the issue, i think it's was syntax problem and the new query that's working like a charm is below



select users.firstName,users.lastName,users.phoneNumber,count(CASE
WHEN comments.`commentType` = "spam" THEN 1 ELSE NULL END) as spamCounter,
ROUND(count(CASE
WHEN comments.`commentType` = "spam" THEN 1 ELSE NULL END)/count(orders.id),2) AS ratio_spam,
count(orders.id) as totalOrder
from users,providers,orders
LEFT JOIN comments ON `orders`.`id`= `comments`.`commentableId`
where
orders.`providerId` = providers.id
and
users.id = providers.userId
and
`orders`.`createdAt` >= (CURDATE() - INTERVAL 7 DAY)
GROUP BY users.id
ORDER BY spamCounter DESC
LIMIT 20;





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',
    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%2f53403468%2fmysql-left-join-with-count-returns-unknown-column%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    i fixed the issue, i think it's was syntax problem and the new query that's working like a charm is below



    select users.firstName,users.lastName,users.phoneNumber,count(CASE
    WHEN comments.`commentType` = "spam" THEN 1 ELSE NULL END) as spamCounter,
    ROUND(count(CASE
    WHEN comments.`commentType` = "spam" THEN 1 ELSE NULL END)/count(orders.id),2) AS ratio_spam,
    count(orders.id) as totalOrder
    from users,providers,orders
    LEFT JOIN comments ON `orders`.`id`= `comments`.`commentableId`
    where
    orders.`providerId` = providers.id
    and
    users.id = providers.userId
    and
    `orders`.`createdAt` >= (CURDATE() - INTERVAL 7 DAY)
    GROUP BY users.id
    ORDER BY spamCounter DESC
    LIMIT 20;





    share|improve this answer

























      up vote
      0
      down vote



      accepted










      i fixed the issue, i think it's was syntax problem and the new query that's working like a charm is below



      select users.firstName,users.lastName,users.phoneNumber,count(CASE
      WHEN comments.`commentType` = "spam" THEN 1 ELSE NULL END) as spamCounter,
      ROUND(count(CASE
      WHEN comments.`commentType` = "spam" THEN 1 ELSE NULL END)/count(orders.id),2) AS ratio_spam,
      count(orders.id) as totalOrder
      from users,providers,orders
      LEFT JOIN comments ON `orders`.`id`= `comments`.`commentableId`
      where
      orders.`providerId` = providers.id
      and
      users.id = providers.userId
      and
      `orders`.`createdAt` >= (CURDATE() - INTERVAL 7 DAY)
      GROUP BY users.id
      ORDER BY spamCounter DESC
      LIMIT 20;





      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        i fixed the issue, i think it's was syntax problem and the new query that's working like a charm is below



        select users.firstName,users.lastName,users.phoneNumber,count(CASE
        WHEN comments.`commentType` = "spam" THEN 1 ELSE NULL END) as spamCounter,
        ROUND(count(CASE
        WHEN comments.`commentType` = "spam" THEN 1 ELSE NULL END)/count(orders.id),2) AS ratio_spam,
        count(orders.id) as totalOrder
        from users,providers,orders
        LEFT JOIN comments ON `orders`.`id`= `comments`.`commentableId`
        where
        orders.`providerId` = providers.id
        and
        users.id = providers.userId
        and
        `orders`.`createdAt` >= (CURDATE() - INTERVAL 7 DAY)
        GROUP BY users.id
        ORDER BY spamCounter DESC
        LIMIT 20;





        share|improve this answer












        i fixed the issue, i think it's was syntax problem and the new query that's working like a charm is below



        select users.firstName,users.lastName,users.phoneNumber,count(CASE
        WHEN comments.`commentType` = "spam" THEN 1 ELSE NULL END) as spamCounter,
        ROUND(count(CASE
        WHEN comments.`commentType` = "spam" THEN 1 ELSE NULL END)/count(orders.id),2) AS ratio_spam,
        count(orders.id) as totalOrder
        from users,providers,orders
        LEFT JOIN comments ON `orders`.`id`= `comments`.`commentableId`
        where
        orders.`providerId` = providers.id
        and
        users.id = providers.userId
        and
        `orders`.`createdAt` >= (CURDATE() - INTERVAL 7 DAY)
        GROUP BY users.id
        ORDER BY spamCounter DESC
        LIMIT 20;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 12 hours ago









        Faisal

        599




        599






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403468%2fmysql-left-join-with-count-returns-unknown-column%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







            Popular posts from this blog

            Berounka

            Sphinx de Gizeh

            Different font size/position of beamer's navigation symbols template's content depending on regular/plain...