Trying to get a count from multiple mysql tables












-1















I have been improving my MYSQL skill over the past month but have hit the point where im unsure of what to do get this over the line.



select id, fname,lname,status, email, country_name, login_count, 
(select count(ID) from applications inner join players on users.id = players.user_id inner join applications on players.id = applications.user_id where players.id = applications.player_id), (select count(ID) from messages where users.id = messages.user_id) AS '# of Messages', (select count(id) from participants where users.id = participants.user_id) AS '# of Threads', (SELECT created_at FROM messages where users.id = messages.user_id ORDER BY id DESC LIMIT 1 ) AS 'Last Message Date', updated_at
from users
where type = 'player'
#And (country_name = 'australia' or country_name = 'new zealand' or country_name = 'South Africa')
and status = 'active'
and (select count(ID) from messages where users.id = messages.user_id) = 0
and (select count(id) from participants where users.id = participants.user_id) != 0
and updated_at > '2018-10-01'
order by updated_at desc


I am trying to get the number of applications that a user of type player has made. Im also keen to get the number of messages and threads they have been involved in.



I can run the query fine if i remove




(select count(ID) from applications inner join players on users.id = players.user_id inner join applications on players.id = applications.user_id where players.id = applications.player_id),




but as soon as i try to get a count of the applications made it returns this




Not unique table/alias: 'applications'




Any help would be most appreciated.



(please be kind, I'm self-teaching myself SQL)










share|improve this question























  • See meta.stackoverflow.com/questions/333952/…

    – Strawberry
    Nov 15 '18 at 23:46
















-1















I have been improving my MYSQL skill over the past month but have hit the point where im unsure of what to do get this over the line.



select id, fname,lname,status, email, country_name, login_count, 
(select count(ID) from applications inner join players on users.id = players.user_id inner join applications on players.id = applications.user_id where players.id = applications.player_id), (select count(ID) from messages where users.id = messages.user_id) AS '# of Messages', (select count(id) from participants where users.id = participants.user_id) AS '# of Threads', (SELECT created_at FROM messages where users.id = messages.user_id ORDER BY id DESC LIMIT 1 ) AS 'Last Message Date', updated_at
from users
where type = 'player'
#And (country_name = 'australia' or country_name = 'new zealand' or country_name = 'South Africa')
and status = 'active'
and (select count(ID) from messages where users.id = messages.user_id) = 0
and (select count(id) from participants where users.id = participants.user_id) != 0
and updated_at > '2018-10-01'
order by updated_at desc


I am trying to get the number of applications that a user of type player has made. Im also keen to get the number of messages and threads they have been involved in.



I can run the query fine if i remove




(select count(ID) from applications inner join players on users.id = players.user_id inner join applications on players.id = applications.user_id where players.id = applications.player_id),




but as soon as i try to get a count of the applications made it returns this




Not unique table/alias: 'applications'




Any help would be most appreciated.



(please be kind, I'm self-teaching myself SQL)










share|improve this question























  • See meta.stackoverflow.com/questions/333952/…

    – Strawberry
    Nov 15 '18 at 23:46














-1












-1








-1








I have been improving my MYSQL skill over the past month but have hit the point where im unsure of what to do get this over the line.



select id, fname,lname,status, email, country_name, login_count, 
(select count(ID) from applications inner join players on users.id = players.user_id inner join applications on players.id = applications.user_id where players.id = applications.player_id), (select count(ID) from messages where users.id = messages.user_id) AS '# of Messages', (select count(id) from participants where users.id = participants.user_id) AS '# of Threads', (SELECT created_at FROM messages where users.id = messages.user_id ORDER BY id DESC LIMIT 1 ) AS 'Last Message Date', updated_at
from users
where type = 'player'
#And (country_name = 'australia' or country_name = 'new zealand' or country_name = 'South Africa')
and status = 'active'
and (select count(ID) from messages where users.id = messages.user_id) = 0
and (select count(id) from participants where users.id = participants.user_id) != 0
and updated_at > '2018-10-01'
order by updated_at desc


I am trying to get the number of applications that a user of type player has made. Im also keen to get the number of messages and threads they have been involved in.



I can run the query fine if i remove




(select count(ID) from applications inner join players on users.id = players.user_id inner join applications on players.id = applications.user_id where players.id = applications.player_id),




but as soon as i try to get a count of the applications made it returns this




Not unique table/alias: 'applications'




Any help would be most appreciated.



(please be kind, I'm self-teaching myself SQL)










share|improve this question














I have been improving my MYSQL skill over the past month but have hit the point where im unsure of what to do get this over the line.



select id, fname,lname,status, email, country_name, login_count, 
(select count(ID) from applications inner join players on users.id = players.user_id inner join applications on players.id = applications.user_id where players.id = applications.player_id), (select count(ID) from messages where users.id = messages.user_id) AS '# of Messages', (select count(id) from participants where users.id = participants.user_id) AS '# of Threads', (SELECT created_at FROM messages where users.id = messages.user_id ORDER BY id DESC LIMIT 1 ) AS 'Last Message Date', updated_at
from users
where type = 'player'
#And (country_name = 'australia' or country_name = 'new zealand' or country_name = 'South Africa')
and status = 'active'
and (select count(ID) from messages where users.id = messages.user_id) = 0
and (select count(id) from participants where users.id = participants.user_id) != 0
and updated_at > '2018-10-01'
order by updated_at desc


I am trying to get the number of applications that a user of type player has made. Im also keen to get the number of messages and threads they have been involved in.



I can run the query fine if i remove




(select count(ID) from applications inner join players on users.id = players.user_id inner join applications on players.id = applications.user_id where players.id = applications.player_id),




but as soon as i try to get a count of the applications made it returns this




Not unique table/alias: 'applications'




Any help would be most appreciated.



(please be kind, I'm self-teaching myself SQL)







mysql






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 22:24









Glenn CoyleGlenn Coyle

31




31













  • See meta.stackoverflow.com/questions/333952/…

    – Strawberry
    Nov 15 '18 at 23:46



















  • See meta.stackoverflow.com/questions/333952/…

    – Strawberry
    Nov 15 '18 at 23:46

















See meta.stackoverflow.com/questions/333952/…

– Strawberry
Nov 15 '18 at 23:46





See meta.stackoverflow.com/questions/333952/…

– Strawberry
Nov 15 '18 at 23:46












1 Answer
1






active

oldest

votes


















0














Your issue is when you run the subquery here:



select count(ID)
from applications
inner join players on users.id = players.user_id
inner join applications on players.id = applications.user_id
where players.id = applications.player_id


Any time that you reference a table more than once in a given query, you need to give that table reference a unique alias. This query touches the applications table twice, so MySQL doesn't know which one you're referring to in a given situation (and in general, you should be aliasing your tables anyway, since it makes EXPLAIN statements easier to follow for debugging, and makes the query more readable).



You can add aliases using the as syntax:



select
count(mainApps.ID)
from
applications as mainApps
inner join
players as appPlayers
on appPlayers.id = mainApps.playerId
inner join
applications as joinedApps
on appPlayers.id = joinedApps.user_id
where
users.id = appPlayers.user_id





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%2f53328727%2ftrying-to-get-a-count-from-multiple-mysql-tables%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









    0














    Your issue is when you run the subquery here:



    select count(ID)
    from applications
    inner join players on users.id = players.user_id
    inner join applications on players.id = applications.user_id
    where players.id = applications.player_id


    Any time that you reference a table more than once in a given query, you need to give that table reference a unique alias. This query touches the applications table twice, so MySQL doesn't know which one you're referring to in a given situation (and in general, you should be aliasing your tables anyway, since it makes EXPLAIN statements easier to follow for debugging, and makes the query more readable).



    You can add aliases using the as syntax:



    select
    count(mainApps.ID)
    from
    applications as mainApps
    inner join
    players as appPlayers
    on appPlayers.id = mainApps.playerId
    inner join
    applications as joinedApps
    on appPlayers.id = joinedApps.user_id
    where
    users.id = appPlayers.user_id





    share|improve this answer




























      0














      Your issue is when you run the subquery here:



      select count(ID)
      from applications
      inner join players on users.id = players.user_id
      inner join applications on players.id = applications.user_id
      where players.id = applications.player_id


      Any time that you reference a table more than once in a given query, you need to give that table reference a unique alias. This query touches the applications table twice, so MySQL doesn't know which one you're referring to in a given situation (and in general, you should be aliasing your tables anyway, since it makes EXPLAIN statements easier to follow for debugging, and makes the query more readable).



      You can add aliases using the as syntax:



      select
      count(mainApps.ID)
      from
      applications as mainApps
      inner join
      players as appPlayers
      on appPlayers.id = mainApps.playerId
      inner join
      applications as joinedApps
      on appPlayers.id = joinedApps.user_id
      where
      users.id = appPlayers.user_id





      share|improve this answer


























        0












        0








        0







        Your issue is when you run the subquery here:



        select count(ID)
        from applications
        inner join players on users.id = players.user_id
        inner join applications on players.id = applications.user_id
        where players.id = applications.player_id


        Any time that you reference a table more than once in a given query, you need to give that table reference a unique alias. This query touches the applications table twice, so MySQL doesn't know which one you're referring to in a given situation (and in general, you should be aliasing your tables anyway, since it makes EXPLAIN statements easier to follow for debugging, and makes the query more readable).



        You can add aliases using the as syntax:



        select
        count(mainApps.ID)
        from
        applications as mainApps
        inner join
        players as appPlayers
        on appPlayers.id = mainApps.playerId
        inner join
        applications as joinedApps
        on appPlayers.id = joinedApps.user_id
        where
        users.id = appPlayers.user_id





        share|improve this answer













        Your issue is when you run the subquery here:



        select count(ID)
        from applications
        inner join players on users.id = players.user_id
        inner join applications on players.id = applications.user_id
        where players.id = applications.player_id


        Any time that you reference a table more than once in a given query, you need to give that table reference a unique alias. This query touches the applications table twice, so MySQL doesn't know which one you're referring to in a given situation (and in general, you should be aliasing your tables anyway, since it makes EXPLAIN statements easier to follow for debugging, and makes the query more readable).



        You can add aliases using the as syntax:



        select
        count(mainApps.ID)
        from
        applications as mainApps
        inner join
        players as appPlayers
        on appPlayers.id = mainApps.playerId
        inner join
        applications as joinedApps
        on appPlayers.id = joinedApps.user_id
        where
        users.id = appPlayers.user_id






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 23:04









        Jpec07Jpec07

        3136




        3136






























            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%2f53328727%2ftrying-to-get-a-count-from-multiple-mysql-tables%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

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)