COUNT multiple records from different tables MYSQL - php

I have this two mysql tables
main_date_family
main_date_non_family
I want to count duplicate records from two tables like this
---------------------
| id_file | c |
---------------------
| 1 | 3 |
| 6 | 2 |
| 14 | 5 |

You need to use UNION ALL to achieve the output:
SELECT
t.id_file,
COUNT(*) AS c
FROM
(
SELECT id_file FROM main_date_family
UNION ALL
SELECT id_file FROM main_date_non_family
) AS t
GROUP BY t.id_file
EDIT:
SELECT
t.id,
COUNT(t.id) AS c
FROM
(
SELECT id_file AS id FROM main_date_family
UNION ALL
SELECT id_file FROM main_date_non_family
) AS t
WHERE t.id IN ( SELECT A.id_file
FROM main_date_family A
INNER JOIN main_date_non_family B ON A.id_file = B.id_file
)
GROUP BY t.id
Explaining the idea:
Get all the common id_files.
Now combine all the id_files from those two tables. Mind it that
this combination holds all the id_files from those two tables as
though it's a single table.
Now get only those id_files from second step which are found in
the first step.
Now the only step left is the GROUP BY & COUNT. I guess
you know well what's group by

Related

mysql join query didnt get correct out put

I have 3 tables
admin_courses
Admin_course_groups
Group_permision
admin_courses
c_id | c_name | c_status
1 | test1 | 1
2 |test2 |1
3 |test3 |1
4 test4 1
Admin_course_groups
a_id | fk_c_id |fk_g_id |start_date |end_date
1 | 1 | 1 | 2018-10-10 |2018-10-20
2 | 5 |1 | 2018-10-10 | 2018-10-20
3 | 4 |3 |2018-10-10 |2018-10-20
Group_permision
gp_id|fk_g_id|user_id
1 1 2
2 3 2
2 1 3
Here total four courses added i want to know how many course assign to each user, i have query
SELECT c_id
, c_name
, COUNT(a_id) AS nam
, MIN(start_date) as start_date
, MIN(end_date) as end_date
FROM admin_courses c
LEFT
JOIN Admin_course_groups g
ON g.fk_c_id = c.c_id
left
join Group_permision h
on g. fk_g_id=h.fk_g_id
and users_id=3
where c.c_status=1
GROUP
BY c_id
So here it will return all course, if course is assigned for user COUNT(a_id) will 1 else it 0.
Now my issue is that if users_id is 3 user assigned only 1 course but i got 3 instead of 1.
Please help me. any help would be appreciated.
You are counting wrong field. To count number of Users in a Course, you need to count user_id; Also, use Count(Distinct ...) to avoid counting a same user multiple times.
Also, note that I have added c.c_name to Group By clause, to be compatible with only_full_group_by mode. Do Read: SELECT list is not in GROUP BY clause and contains nonaggregated column .... incompatible with sql_mode=only_full_group_by
You dont need to join on user_id, to count total number of users. It restricts your data.
Try:
SELECT c.c_id
, c.c_name
, COUNT(DISTINCT h.user_id) AS nam
, MIN(g.start_date) AS start_date
, MIN(g.end_date) AS end_date
FROM admin_courses AS c
LEFT
JOIN Admin_course_groups AS g
ON g.fk_c_id = c.c_id
LEFT
JOIN Group_permision AS h
on g.fk_g_id = h.fk_g_id
WHERE c.c_status=1
GROUP
BY c.c_id,
c.c_name
Your question is missing some crucial information, but if I understood the problem correctly, I would simply try to add Distinct to the count :
SELECT `c_id`, `c_name`, COUNT(distinct `a_id`)
....

MySQL 'Group By' messes up virtual column count

I am trying to select distinct rows within my SQL table, however I'm not having luck in labeling the returned rows appropriately using the code below:
SELECT #row:=#row+1 as rank,
a.id,
a.name
FROM table a,
( SELECT #row:=0) b
GROUP BY a.id
ORDER BY a.name ASC
This query will return the following:
| RANK | ID | NAME
--------------------------
2 | 4483 | Bob
8 | 9453 | Joe
10 | 4543 | Maurice
What I want it to return is this, however:
| RANK | ID | NAME
--------------------------
1 | 4483 | Bob
2 | 9453 | Joe
3 | 4543 | Maurice
Would it be more appropriate for me to use a DISTINCT query for a query of this magnitude?
As per Marc B's solution, I decided to wrap my query with another one however instead I decided to Select DISTINCT columns rather than grouping them which would remove my margin of error, by using this code
SELECT #row:=#row+1 as rank, a.id, a.name FROM
(
SELECT DISTINCT id, name
FROM Table1
) a, (SELECT #row:=0) b
ORDER BY a.name ASC

Mysql Count to return zero if no match

I have a set of approx 9000 tutor ids in an array and i have put them in a string like:
(1,2, 3, 4,5,6,7,....9000,9001,9002)
so that i can use them in the following query:
select count(student_assignment.assignment_id) as total_assignment from
student_assignment, assigned_tutor_fk where assignment_status = 'closed'
and assigned_tutor_fk in (1,2, 3, 4,5,6,7,..100,101,103...9000,9001,9002)
group by assigned tutor_fk.
I want to calculate total number of rows associated with each tutor(assigned_tutor_fk), and those tutors which do not have an assignment ie those which do not have assignment
record in the table i want to show their assignment count as 0, and i just want my query to return count and assigned_tutor_fk
my table structure is:
assignment_id | assigned_tutor_fk | assignment_date | student_id |
| 1 | 2 | 22-01-2011 | 4 |
| 2 | 3 | 14-03-2011 | 5 |
Im trying to get my output to be like this:
|total_assignment | assigned_tutor_fk |
| 5 | 4 |
| 2 | 7 |
| 0 | 8 |
Update: I tthink i have not been able to express myself properly,i already have a list of tutors filtered on another criteria, it was very complex to combine these two queries so now i have a set of the tutor id's and i want the sum to be displayed as zero in case the tutors does not have assignment record. please help me on this as i don know wht to do now
SELECT t.id, COUNT(sa.assignment_id)
FROM tutor t
LEFT JOIN
student_assignement sa
ON sa.assignment_tutor_fk = t.id
WHERE t.id IN (1, 2, ..., 9002)
GROUP BY
t.id
dont put the tutors in a string. Select them from a table and do a LEFT JOIN with the assignment and FK table. Without knowing all of your tables, i'm guessing it would look like this:
select
t.tutorId,
count(sa.assignment_id) as total_assignment
from
tutor t
LEFT JOIN
assigned_tutor_fk fk
ON
fk.assigned_tutor_fk = tutor.tutorId
LEFT JOIN
student_assignment sa
ON
fk.assignment_id = sa.id
where
sa.assignment_status = 'closed' OR
ISNULL(sa.assignment_status) -- if join fails.
group by
t.tutorId
Left Join retrieves all your values from the tutor table and merges it with the joined table IF there is a match. If not, NULL is inserted.
SELECT
count(*) as total_assignment,
assigned_tutor_fk
FROM assignmentTable
GROUP BY assigned_tutor_fk

Insert Comma Separated values through MySQL query

I have one table (in phpmyadmin) with the following fields and structure:
Table 1
id | sponsor
1 | -1
2 | 1
3 | 1
4 | 2
5 | 4
6 | 4
Now i want to insert data from above table into new table as below:
Table 2
id | children
1 | 2,3
2 | 4
3 |
4 | 5,6
5 |
6 |
Actually this is Tree structure, which i have saved in mysql database.
I have already written a script in php but as there are more then 100K rows in table 1 so its taking too much time. Please tell me an efficient sql query to do this task quickly.
Query:
SQLFIDDLE Example
SELECT
t1.id,
(SELECT group_concat(id separator ', ')
FROM table1 t2
WHERE t2.sponsor = t1.id) AS children
FROM table1 t1
GROUP BY t1.id
Result:
| ID | CHILDREN |
-----------------
| 1 | 2, 3 |
| 2 | 4 |
| 3 | (null) |
| 4 | 5, 6 |
| 5 | (null) |
| 6 | (null) |
Insert Statement:
INSERT INTO table2
SELECT
t1.id,
(SELECT group_concat(id separator ', ')
FROM table1 t2
WHERE t2.sponsor = t1.id) AS children
FROM table1 t1
GROUP BY t1.id
This is similar to #Justin's answer but uses a left join instead of a correlated subquery:
INSERT INTO Table2 (id, children)
SELECT
sp.id,
GROUP_CONCAT(ch.id) AS children
FROM Table1 sp
LEFT JOIN Table1 ch ON sp.id = ch.sponsor
GROUP BY t1.id
;
A demonstration of the SELECT statement's result can be found (and played with) at SQL Fiddle (the schema having been borrowed from Justin).
One of your SELECT elements should be a GROUP_CONCAT(...) as column, which will concatenate those values separated with commas. If you want to filter by one of those values, you can use GROUP BY -whatever- HAVING find_in_set( -number- , column )
See if the following helps
INSERT INTO table2
SELECT sponsor, GROUP_CONCAT(id)
FROM table1
GROUP BY id

Count occurrences of distinct values in 2 fields

I am trying to find a MySQL query that will find distinct values in a particular field, count the number of occurrences of that value in 2 fields (1_user, 2_user) and then order the results by the count.
example db
+------+-----------+-----------+
| id | 1_user | 2_user |
+------+-----------+-----------+
| 1 | 2 | 1 |
| 2 | 3 | 2 |
| 3 | 8 | 7 |
| 4 | 1 | 8 |
| 5 | 2 | 8 |
| 6 | 3 | 8 |
+------+-----------+-----------+
expected result
user count
----- -----
8 4
2 3
3 2
1 2
The Query
SELECT user, count(*) AS count
FROM
(
SELECT 1_user AS USER FROM test
UNION ALL
SELECT 2_user FROM test
) AS all_users
GROUP BY user
ORDER BY count DESC
Explanation
List all the users in the first column.
SELECT 1_user AS USER FROM test
Combine them with the users from the second column.
UNION ALL
SELECT 2_user FROM test
The trick here is the UNION ALL which preserves duplicate values.
The rest is easy -- select the results you want from the subquery:
SELECT user, count(*) AS count
aggregate by user:
GROUP BY user
and prescribe the order:
ORDER BY count DESC
SELECT u, count(u) AS cnt
FROM (
SELECT 1_user AS u FROM table
UNION ALL
SELECT 2_user AS u FROM table
) subquery
GROUP BY u
ORDER by cnt DESC
Take the 2 queries:
SELECT COUNT(*) FROM table GROUP BY 1_user
SELECT COUNT(*) FROM table GROUP BY 2_user
Now combine them:
SELECT user, SUM(count) FROM
((SELECT 1_user as user FROM table)
UNION ALL
(SELECT 2_user as user FROM table))
GROUP BY user, ORDER BY count DESC;
I think this what you are looking for since your expected result did not include 7
select usr, count(usr) cnt from
(
select user_1 usr from users
union all
select user_2 usr from users
) u
where u.usr in (select user_1 from users)
group by usr
order by count(u.usr) desc

Categories