Trying to get a count from multiple mysql tables
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
add a comment |
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
See meta.stackoverflow.com/questions/333952/…
– Strawberry
Nov 15 '18 at 23:46
add a comment |
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
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
mysql
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
answered Nov 15 '18 at 23:04
Jpec07Jpec07
3136
3136
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
See meta.stackoverflow.com/questions/333952/…
– Strawberry
Nov 15 '18 at 23:46