MYSQL - Count total of rows from multiple tables with same column name - mysql

I have three tables: a_survey, b_survey and c_survey. Each of these tables have a column_name "user_id". What I want to do is count the actual posted surveys and return that value in a single row. So, if there was found one record with the matching user_id for each of these tables, the count would return 3.
Here's what I've tried:
select count(user_id) AS total_of_surveys
FROM a.survey, b.survey, c.survey
WHERE user_id = 3;
but I'm getting error:
Column 'user_id' in field list is ambiguous
Thanks in advance!

select sum(count(a.survey.user_id)+count(b.survey.user_id)+count(c.survey.user_id)) AS total_of_surveys
FROM a.survey, b.survey, c.survey
WHERE user_id = 3;

What you have tried was a cartesian plan it will multiply the result by the quantity of registries on each table.
The right way to do what you want is to join (with left join) or 'UNION' the tables like:
select count(user_id) from
(select user_id from a_survey where user_id = 3 UNION ALL
select user_id from b_survey where user_id = 3 UNION ALL
select user_id from c_survey where user_id = 3) tables

You will need to count each table seperately and sum it together. You can do this with one (ugly) query if you want. Something like this should work:
Select (select count(user_id) FROM a.survey WHERE user_id = 3) + (select count(user_id) FROM b.survey WHERE user_id = 3) + (select count(user_id) FROM c.survey WHERE user_id = 3) as total_of_surveys;

Related

return the same record more than once in a query result

The following query:
SELECT * FROM table WHERE id IN (1,2,3);
will return three records.
SELECT * FROM table WHERE id IN (1,2,1);
will return just two records (for Ids 1 and 2)
Is there a way for the result set to contain two records for Id 1 (and three in total)?
You could try creating a table for the ids you want to filter by. This would get you your desired results. I'm not sure if mysql supports CTE, but hopefully this is enough for you to get the idea.
WITH IDS
AS
(
SELECT 1 AS id
UNION ALL
SELECT 2 AS id
UNION ALL
SELECT 1 AS id
)
SELECT T.*
FROM T
JOIN IDS
ON T.id = IDS.id

Query to find users with one row in table

I have a table "users" which has multiple columns in which column "status" has multiple values like 1,0,3,2,4. There is column "user_id" which doesn't contain unique values since, this is foreign key of another table called "user_master".
so here in "users" table we have multiple values of the one user.
So, Here is my actual query is that i would like to write a sql query to find users has only one entry in table "users" with particular status value.
For e.g. I would like to fetch all such users with status=2 and their entry in table is not more than 1. Like if user has multiple entries with status 2,1,4 in table which should not be return in query.
It should yield those users which has only one entry in table and which is of status = 2
That must be what you use:
Select count(u.user_id) AS cnt, u.*
from user u
where u.status = 2
group by u.user_id, u.status
having cnt = 1;
WITH tmp AS(
SELECT Stud_Id,COUNT(*) AS 'Count' FROM Student_tbl GROUP BY Stud_Id
)
SELECT * FROM tmp WHERE Count = 1 AND Status = 2
You have to add field in GROUP BY Clause whichever you want to use in SELECT clause.
I have researched it and found answer for it.
And query goes like this.
select count(id) as cnt,
user_id,status from users
group by user_id
having cnt < 2 and status=2
First it will group the things having count less than 2 and then which will check for status.

MySQL view count and sum and other operations from multiple tables

Here is my situation:
I have 4 tables that all contains a column called score in all of these tables my goal for a view to create operations to the result of the 4 tables getting the following values:
Total score
Total number of rows
average (total score / number of rows)
Now i know that i would be able to create the view as:
(SELECT * FROM table1 where condition) + (SELECT * FROM table2 where condition)
So on and so forth.
but for each of the three goals i have i would have to nested select all tables atleast 2 times.
So my question is how do you handle a case like this? is there any operation in sql that makes this an easy task or am i bound to do something redundant?
Update
So my full case is that every use in my system has something called a division_id now i want to use this ID to find out what the score is for each division:
(PLEASE IGNORE THE _COPY)
You could use a UNION to join the 4 tables, since there is no join condition. There are a couple of ways that you could do this with the division field. Probably the most concise is:
select division_id, count(*), avg(scores.score), sum(scores.score) from
user join
(select id as user_id, score from user
UNION ALL
select user_id, score from test_score
UNION ALL
select user_id, score from task_score
UNION ALL
select user_id, score from offline_score) as scores
on user.id = scores.user_id
group by division_id
Link to SQLFiddle

Obtain a list with the items found the minimum amount of times in a table

I have a MySQL table where I have a certain id as a foreign key coming from another table. This id is not unique to this table so I can have many records holding the same id.
I need to find out which ids are seen the least amount of times in this table and pull up a list containing them.
For example, if I have 5 records with id=1, 3 records with id=2 and 3 records with id=3, I want to pull up only ids 2 & 3. However, the data in the table changes quite often so I don't know what that minimum value is going to be at any given moment. The task is quite trivial if I use two queries but I'm trying to do it with just one. Here's what I have:
SELECT id
FROM table
GROUP BY id
HAVING COUNT(*) = MIN(SELECT COUNT(*) FROM table GROUP BY id)
If I substitute COUNT(*) = 3, then the results come up but using the query above gives me an error that MIN is not used properly. Any tips?
I would try with:
SELECT id
FROM table
GROUP BY id
HAVING COUNT(*) = (SELECT COUNT(*) FROM table GROUP BY id ORDER BY COUNT(*) LIMIT 1);
This gets the minimum selecting the first row from the set of counts in ascendent order.
You need a double select in the having clause:
SELECT id
FROM table
GROUP BY id
HAVING COUNT(*) = (SELECT MIN(cnt) FROM (SELECT COUNT(*) as cnt FROM table GROUP BY id) t);
The MIN() aggregate function is suposed to take a column, not a query. So, I see two ways to solve this:
To properly write the subquery, or
To use temp variables
First alternative:
select id
from yourTable
group by id
having count(id) = (
select min(c) from (
select count(*) as c from yourTable group by id
) as a
)
Second alternative:
set #minCount = (
select min(c) from (
select count(*) as c from yourTable group by id
) as a
);
select id
from yourTable
group by id
having count(*) = #minCount;
You need to GROUP BY to produce a set of grouped values and additional select to get the MIN value from that group, only then you can match it against having
SELECT * FROM table GROUP BY id
HAVING COUNT(*) =
(SELECT MIN(X.CNT) AS M FROM(SELECT COUNT(*) CNT FROM table GROUP BY id) AS X)

Select Data From Table Without Multiple Of The Same User ID

I am trying to select all the elements where x_id=1 but there will be multiple rows for that result with the same user_id and I just want it to show one result for each user id (instead of multiple). How would I be able to do this in SQL im completely lost?
Table:
a
id | x_id | user_id
SELECT DISTINCT user_id FROM table WHERE x_id = 1;
select distinct user_id from a where x_id = 1;
SELECT a.user_id
FROM a a
WHERE a.x_id = 1
GROUP BY a.user_id
-- or --
SELECT DISTINCT a.user_id
FROM a a
WHERE a.x_id = 1
These return an equivalent result set. My personal preference is to use the GROUP BY, because I sometimes want to return a count of the number of rows, or use some other aggregate function, which I can't do with the DISTINCT.