How to retrieve records with duplicate in mysql - mysql

Im having problem retrieving the PLAYERs that has PLAYER_SCOREs that has duplicate PLAYER_SCORE. The criteria for PLAYER_SCORE to be considered duplicate it has other record that has the same P_ID, SCORE_1, SCORE_2, SCORE_3, and SCORE_4.
PLAYER TABLE:
+------+---------+
| P_ID | NAME |
+------+---------+
| 12 | Juan |
| 13 | Miguel |
| 14 | Luna |
| 15 | Placido |
+------+---------+
PLAYER SCORE TABLE
+-------+------+---------+---------+---------+---------+
| PS_ID | P_ID | SCORE_1 | SCORE_2 | SCORE_3 | SCORE_4 |
+-------+------+---------+---------+---------+---------+
| 1 | 1 | 87 | 96 | 79 | 93 |
| 2 | 1 | 87 | 96 | 97 | 88 |
| 3 | 1 | 87 | 96 | 79 | 93 |
| 4 | 2 | 85 | 84 | 85 | 94 |
| 5 | 2 | 87 | 96 | 22 | 44 |
| 6 | 2 | 85 | 84 | 85 | 94 |
| 7 | 3 | 79 | 96 | 82 | 84 |
| 8 | 3 | 97 | 96 | 92 | 95 |
| 9 | 3 | 87 | 96 | 97 | 87 |
| 10 | 4 | 89 | 75 | 99 | 97 |
| 11 | 4 | 97 | 96 | 92 | 95 |
| 12 | 4 | 87 | 96 | 97 | 87 |
+-------+------+---------+---------+---------+---------+
My sql scripts:
Script 1 :
SELECT P.P_ID, NAME FROM PLAYER P
INNER JOIN PLAYER_SCORE PS ON PS.P_ID = P.P_ID
GROUP BY P_ID, SCORE_1, SCORE_2, SCORE_3, SCORE_4
RESULT:
+------+---------+
| P_ID | NAME |
+------+---------+
| 1 | Juan |
| 1 | Juan |
| 2 | Miguel |
| 2 | Miguel |
| 3 | Luna |
| 3 | Luna |
| 3 | Luna |
| 4 | Placido |
| 4 | Placido |
| 4 | Placido |
+------+---------+
Script 2 :
SELECT P_ID, NAME FROM (
SELECT P.P_ID, NAME FROM PLAYER P
INNER JOIN PLAYER_SCORE PS ON PS.P_ID = P.P_ID
GROUP BY P_ID, SCORE_1, SCORE_2, SCORE_3, SCORE_4
) AS PLAYER GROUP BY P_ID
Result :
+------+---------+
| P_ID | NAME |
+------+---------+
| 1 | Juan |
| 2 | Miguel |
| 3 | Luna |
| 4 | Placido |
+------+---------+
Expected :
+------+--------+
| P_ID | NAME |
+------+--------+
| 1 | Juan |
| 2 | Miguel |
+------+--------+
Any help... Thanks.

You should keep only duplicates, so restrict output with HAVING:
SELECT P.P_ID, NAME FROM PLAYER P
INNER JOIN PLAYER_SCORE PS ON PS.P_ID = P.P_ID
GROUP BY P_ID, NAME, SCORE_1, SCORE_2, SCORE_3, SCORE_4
HAVING count(*) > 1

You can try this way :
SELECT P_ID, NAME FROM
PLAYER INNER JOIN
(
SELECT P_ID FROM PLAYER_SCORE
GROUP BY P_ID, SCORE_1, SCORE_2, SCORE_3, SCORE_4
HAVING COUNT(*) > 1
) AS SCORE ON SCORE.P_ID = PLAYER.P_ID

Related

How to display both true and false rows in MySQL with IF() condition

Order Table
+---------+------------+----------------+----------+------------+
| OrderID | CustomerID | DateOfPurchase | Discount | DueDate |
+---------+------------+----------------+----------+------------+
| 82 | 7 | 2022-04-17 | 0 | 2022-05-17 |
| 83 | 91 | 2022-04-17 | 0 | 2022-05-17 |
| 84 | 8 | 2022-04-17 | 0 | 2022-05-17 |
| 85 | 91 | 2022-04-17 | 0 | 2022-05-17 |
| 86 | 7 | 2022-04-17 | 0 | 2022-05-17 |
| 87 | 91 | 2022-04-18 | 0 | 2022-05-18 |
| 109 | 7 | 2022-04-25 | 0 | 2022-05-25 |
+---------+------------+----------------+----------+------------+
Customer table
+------------+----------+-------+-------+-------------+-----------------------------+----------+--------------+
| CustomerID | Fname | Mname | Lname | Contact_no | Address | Valid_id | Credit_Limit |
+------------+----------+-------+-------+-------------+-----------------------------+----------+--------------+
| 7 | John | Dale | Doe | 09123654789 | | NULL | 5000.000 |
| 8 | Jane | Dale | Doe | 09987654123 | | NULL | 1500.000 |
| 91 | Kurdapya | Buang | Selos | 09741258963 | | NULL | 5000.000 |
+------------+----------+-------+-------+-------------+-----------------------------+----------+--------------+
Payment table
+-----------+------------+---------+------------+----------+
| PaymentID | CustomerID | OrderID | PayDate | Amount |
+-----------+------------+---------+------------+----------+
| 20 | 7 | 82 | 2022-04-25 | 800.000 |
| 21 | 91 | 83 | 2022-04-17 | 2500.000 |
| 22 | 91 | 85 | 2022-04-17 | 200.000 |
| 23 | 95 | 88 | 2022-04-18 | 2122.000 |
| 24 | 96 | 90 | 2022-04-25 | 577.000 |
| 25 | 97 | 111 | 2022-04-25 | 0.000 |
| 26 | 98 | 114 | 2022-04-25 | 166.000 |
| 27 | 99 | 115 | 2022-04-25 | 1740.000 |
+-----------+------------+---------+------------+----------+
I want to know which are the OrderID of Customer Kurdapya (OrderID=91) that are paid and unpaid
this is the Query I have tried so far
Attempt 1:
select if(py.OrderID=r.OrderID, 'paid','unpaid') as remarks, r.OrderID, r.CustomerID
from orders r,
payment py
where py.OrderID = r.OrderID and r.CustomerID = 91
GROUP by r.OrderID;
Result for attempt 1:
+---------+---------+------------+
| remarks | OrderID | CustomerID |
+---------+---------+------------+
| paid | 83 | 91 |
| paid | 85 | 91 |
+---------+---------+------------+
Attempt 2:
select if(py.OrderID=r.OrderID and py.OrderID=py.Amount!='null', 'paid','unpaid') as remarks, r.OrderID, r.CustomerID
from orders r,
payment py
where r.CustomerID = 91
GROUP by r.OrderID;
Result of Attempt 2:
+---------+---------+------------+
| remarks | OrderID | CustomerID |
+---------+---------+------------+
| unpaid | 83 | 91 |
| unpaid | 85 | 91 |
| unpaid | 87 | 91 |
+---------+---------+------------+
My DESIRED RESULT IS THIS:
+---------+---------+------------+
| remarks | OrderID | CustomerID |
+---------+---------+------------+
| unpaid | 83 | 91 |
| unpaid | 85 | 91 |
| paid | 87 | 91 |
+---------+---------+------------+
SELECT
IF(py.OrderID IS NULL, 'unpaid', 'paid') AS remarks,
r.OrderID,
r.CustomerID
FROM orders AS r
LEFT OUTER JOIN payment AS py USING (OrderID)
WHERE r.CustomerID = 91
However, since the order table doesn't have an amount, I don't know if the amount paid is sufficient to pay for the full order.
You can try
select CASE
WHEN P.Amount > 0 then 'paid' else 'unpaid' end as remarks,
O.orderID, O.CustomerID
from Customer C inner join OrderTable O on C.CustomerID = O.CustomerID
inner join Payment P on P.OrderID = O.OrderID
where O.CustomerID = 91
Your second query didn't even compile.
Try this
select if(py.Amount is not null, 'paid','unpaid') as remarks, r.OrderID, r.CustomerID
from orders r
left outer join payment py on r.OrderID=py.OrderID
where r.CustomerID = 91;
Points to note:
I have used the modern, explicit JOIN syntax - see the comment from #jarlh
I have used LEFT OUTER join
I have removed the quotation marks from 'null' - NULL is a specific value whereas 'null' is a string
The GROUP BY is unecessary

PSQL problem joining multiple tables (MIMIC II database)

I have a patients database with multiple tables, and I need to join specific columns (pre-processed) from different tables in a results table. Here's how I do it:
SELECT d_patients.subject_id, icustay_detail.subject_icustay_seq,
icustay_detail.icustay_id,
icustay_detail.icustay_admit_age, d_patients.sex,
icustay_detail.icustay_los/(60*24) los,
icustay_detail.weight_first/POWER(icustay_detail.height/100,2) bmi,
COALESCE( a.additives, 0 ) as additives,
COALESCE( p.procedureevents, 0 ) as procedureevents,
COALESCE( d.duration, 0 ) as duration,
COALESCE ( m.medications,0 ) as medications
FROM icustay_detail
INNER JOIN d_patients
ON icustay_detail.subject_id = d_patients.subject_id
LEFT JOIN
( SELECT subject_id, count(*) as additives
FROM additives
GROUP BY subject_id
) a
ON icustay_detail.subject_id = a.subject_id
LEFT JOIN
( SELECT subject_id, count(*) as procedureevents
FROM procedureevents
GROUP BY subject_id
) p
ON icustay_detail.subject_id = p.subject_id
LEFT JOIN
( SELECT subject_id, SUM(duration) as duration
FROM a_meddurations
GROUP BY subject_id
) d
ON icustay_detail.subject_id = d.subject_id
LEFT JOIN
( SELECT icustay_id, COUNT(DISTINCT solutionid)
AS medications
FROM medevents
GROUP BY icustay_id
) m
ON icustay_detail.icustay_id = m.icustay_id;
Up until here, the code works fine and the resulting table is the following
subject_id | subject_icustay_seq | icustay_id | icustay_admit_age | sex | los | bmi | additives | procedureevents | duration | medications
------------+---------------------+------------+-------------------+-----+----------------------+------------------+-----------+-----------------+----------+-------------
3 | 1 | 4 | 76.52892 | M | 6.06458333333333 | 30.18767669244 | 10 | 6 | 59145 | 2
12 | 1 | 13 | 72.36841 | M | 7.63472222222222 | | 3 | 10 | 15092 | 2
21 | 1 | 23 | 87.43771 | M | 5.90208333333333 | 21.3569005167836 | 7 | 15 | 0 | 1
21 | 2 | 24 | 87.82761 | M | 8.36527777777778 | | 7 | 15 | 0 | 1
26 | 1 | 29 | 72.00637 | M | 2.47569444444444 | | 0 | 2 | 0 | 0
31 | 1 | 34 | 72.26327 | M | 7.93819444444444 | 22.12581585657 | 6 | 6 | 18316 | 2
37 | 1 | 41 | 68.93134 | M | 1.13958333333333 | | 0 | 2 | 0 | 0
56 | 1 | 62 | 90.6427 | F | 1.84930555555556 | | 3 | 2 | 0 | 0
61 | 1 | 67 | 54.72569 | M | 2.55972222222222 | 18.168292503669 | 1 | 15 | 0 | 0
61 | 2 | 68 | 55.249 | M | 2.04791666666667 | | 1 | 15 | 0 | 1
61 | 3 | 69 | 55.26185 | M | 0.00277777777777778 | | 1 | 15 | 0 | 0
67 | 1 | 76 | 73.48396 | M | 0.214583333333333 | 23.8462015385569 | 2 | 1 | 160 | 1
78 | 1 | 89 | 48.62681 | M | 1.48958333333333 | | 1 | 1 | 2125 | 1
83 | 1 | 94 | 65.54958 | F | 2.30902777777778 | | 0 | 3 | 0 | 0
94 | 1 | 107 | 74.43468 | M | 1.09861111111111 | | 13 | 12 | 0 | 2
94 | 2 | 108 | 74.95603 | M | 22.8979166666667 | | 13 | 12 | 0 | 2
105 | 1 | 121 | 35.33793 | F | 4.99722222222222 | | 2 | 0 | 0 | 0
105 | 2 | 122 | 35.4088 | F | 1.61944444444444 | | 2 | 0 | 0 | 0
106 | 1 | 123 | 35.6095 | M | 6.1625 | 22.4312567927803 | 5 | 1 | 0 | 3
112 | 1 | 136 | 91.96166 | M | 0.840277777777778 | | 5 | 8 | 0 | 0
112 | 2 | 137 | 94.25 | M | 1.13888888888889 | | 5 | 8 | 0 | 2
The problem is when I try to add another column (abnormal) from another table (labevents), which is adding this part:
LEFT JOIN
( SELECT subject_id, round((COUNT(flag) * 100)::numeric / COUNT(*), 2) as abnormal
FROM labevents
GROUP BY subject_id
) ab
ON icustay_detail.subject_id = ab.subject_id
The resulting code is the following:
SELECT d_patients.subject_id, icustay_detail.subject_icustay_seq,
icustay_detail.icustay_id,
icustay_detail.icustay_admit_age, d_patients.sex,
icustay_detail.icustay_los/(60*24) los,
icustay_detail.weight_first/POWER(icustay_detail.height/100,2) bmi,
COALESCE( a.additives, 0 ) as additives,
COALESCE( p.procedureevents, 0 ) as procedureevents,
COALESCE( d.duration, 0 ) as duration,
COALESCE( ab.abnormal, 0 ) as abnormal,
COALESCE( m.medications,0 ) as medications
FROM icustay_detail
INNER JOIN d_patients
ON icustay_detail.subject_id = d_patients.subject_id
LEFT JOIN
( SELECT subject_id, count(*) as additives
FROM additives
GROUP BY subject_id
) a
ON icustay_detail.subject_id = a.subject_id
LEFT JOIN
( SELECT subject_id, count(*) as procedureevents
FROM procedureevents
GROUP BY subject_id
) p
ON icustay_detail.subject_id = p.subject_id
LEFT JOIN
( SELECT subject_id, SUM(duration) as duration
FROM a_meddurations
GROUP BY subject_id
) d
ON icustay_detail.subject_id = d.subject_id
LEFT JOIN
( SELECT subject_id, round((COUNT(flag) * 100)::numeric / COUNT(*), 2) as abnormal
FROM labevents
GROUP BY subject_id
) ab
ON icustay_detail.subject_id = ab.subject_id
LEFT JOIN
( SELECT icustay_id, COUNT(DISTINCT solutionid)
AS medications
FROM medevents
GROUP BY icustay_id
) m
ON icustay_detail.icustay_id = m.icustay_id;
What happens is that the patient rows with 0 value in the "medications" column are removed, and I don't know why. The resulting table is this one:
subject_id | subject_icustay_seq | icustay_id | icustay_admit_age | sex | los | bmi | additives | procedureevents | duration | abnormal | medications
------------+---------------------+------------+-------------------+-----+----------------------+------------------+-----------+-----------------+----------+----------+-------------
3 | 1 | 4 | 76.52892 | M | 6.06458333333333 | 30.18767669244 | 10 | 6 | 59145 | 23.15 | 2
12 | 1 | 13 | 72.36841 | M | 7.63472222222222 | | 3 | 10 | 15092 | 41.85 | 2
21 | 1 | 23 | 87.43771 | M | 5.90208333333333 | 21.3569005167836 | 7 | 15 | 0 | 43.74 | 1
21 | 2 | 24 | 87.82761 | M | 8.36527777777778 | | 7 | 15 | 0 | 43.74 | 1
31 | 1 | 34 | 72.26327 | M | 7.93819444444444 | 22.12581585657 | 6 | 6 | 18316 | 26.69 | 2
61 | 2 | 68 | 55.249 | M | 2.04791666666667 | | 1 | 15 | 0 | 41.63 | 1
67 | 1 | 76 | 73.48396 | M | 0.214583333333333 | 23.8462015385569 | 2 | 1 | 160 | 29.73 | 1
78 | 1 | 89 | 48.62681 | M | 1.48958333333333 | | 1 | 1 | 2125 | 35.75 | 1
94 | 1 | 107 | 74.43468 | M | 1.09861111111111 | | 13 | 12 | 0 | 34.10 | 2
94 | 2 | 108 | 74.95603 | M | 22.8979166666667 | | 13 | 12 | 0 | 34.10 | 2
106 | 1 | 123 | 35.6095 | M | 6.1625 | 22.4312567927803 | 5 | 1 | 0 | 37.67 | 3
112 | 2 | 137 | 94.25 | M | 1.13888888888889 | | 5 | 8 | 0 | 33.67 | 2
117 | 1 | 143 | 49.2842 | F | 1.32013888888889 | | 14 | 15 | 42785 | 47.76 | 1
117 | 2 | 144 | 49.89653 | F | 13.2493055555556 | 40.9070642360035 | 14 | 15 | 42785 | 47.76 | 3
124 | 1 | 151 | 69.63652 | M | 3.91111111111111 | 22.1382505356903 | 7 | 8 | 8560 | 24.24 | 1
124 | 2 | 152 | 71.07388 | M | 7.18958333333333 | | 7 | 8 | 8560 | 24.24 | 1
146 | 1 | 181 | 85.56421 | M | 7.57013888888889 | 34.7959879593229 | 11 | 9 | 32648 | 35.88 | 2
148 | 1 | 183 | 78.1597 | F | 15.2958333333333 | | 7 | 7 | 2826493 | 36.68 | 3
150 | 1 | 185 | 37.82158 | F | 2.26180555555556 | 14.2542487150986 | 1 | 4 | 3083 | 23.10 | 1
157 | 2 | 195 | 80.53706 | M | 4.11944444444444 | | 6 | 11 | 0 | 33.84 | 2
Please let me know if you need the specific content of the tables to try to understand the error. Thank you so much in advance.
Best,
Guille

sql inner join on more than 2 tables and aggregate function

I have the following tables,
select * from department;
+--------------+--------------+--------+------------+---------------+
| departmentid | name | budget | startdate | administrator |
+==============+==============+========+============+===============+
| 101 | Computer Sci | 1000 | 2010-12-26 | XYZ |
| 102 | ECE | 500 | 2015-02-15 | ABC |
| 103 | EEE | 1500 | 2016-08-25 | PQR |
| 104 | Mech | 2500 | 2017-08-22 | LMN |
+--------------+--------------+--------+------------+---------------+
select * from course;
+----------+-------------------+---------+--------------+
| courseid | title | credits | departmentid |
+==========+===================+=========+==============+
| 1001 | Data structures | 12 | 101 |
| 1002 | Algorithms | 12 | 101 |
| 1003 | Graphics | 20 | 101 |
| 2001 | DSP | 20 | 102 |
| 2002 | Matlab | 20 | 102 |
| 2003 | Maths | 10 | 102 |
| 3001 | CAD | 10 | 104 |
| 4001 | Power electronics | 10 | 103 |
| 4002 | Semi conductors | 20 | 103 |
+----------+-------------------+---------+--------------+
select * from student_grade;
+--------------+----------+----------+-------+
| enrollmentid | courseid | personid | grade |
+==============+==========+==========+=======+
| 1 | 1001 | 1 | A |
| 2 | 1002 | 1 | B |
| 3 | 1003 | 1 | A |
| 4 | 3001 | 3 | A |
| 5 | 3001 | 2 | B |
| 6 | 4001 | 4 | A |
| 7 | 4002 | 4 | A |
+--------------+----------+----------+-------+
select * from person;
+----------+----------+-----------+------------+----------------+
| personid | lastname | firstname | hiredate | enrollmentdate |
+==========+==========+===========+============+================+
| 1 | Goudar | Anil | 2016-08-16 | 2016-08-17 |
| 2 | Goudar | Sunil | 2018-09-16 | 2018-09-27 |
| 3 | Dambal | Abhi | 2018-05-07 | 2018-06-17 |
| 4 | Desai | Arun | 2018-05-07 | 2018-06-17 |
| 5 | Xam | Sam | 2018-12-08 | 2018-12-08 |
| 6 | Chatpati | Mangal | 2018-10-10 | 2018-10-08 |
| 9 | Shankar | Dev | 2018-10-10 | 2018-10-08 |
| 10 | Shankar | Mahadev | 2018-08-10 | 2018-08-11 |
+----------+----------+-----------+------------+----------------+
Now I am trying to get the department details with number of students belonging to the department.
And here is my query,
select d.departmentid, d.name, sg.personid from department d inner join course c on c.departmentid = d.departmentid inner join student_grade sg on sg.courseid = c.courseid;
+--------------+--------------+----------+
| departmentid | name | personid |
+==============+==============+==========+
| 101 | Computer Sci | 1 |
| 101 | Computer Sci | 1 |
| 101 | Computer Sci | 1 |
| 104 | Mech | 3 |
| 104 | Mech | 2 |
| 103 | EEE | 4 |
| 103 | EEE | 4 |
+--------------+--------------+----------+
But I want to get the count(distinct(personid)) for each department like group by clause. But I am getting the error with the following query.
select d.departmentid, d.name, count(distinct(sg.personid)) from department d inner join course c on c.departmentid = d.departmentid inner join student_grade sg on sg.courseid = c.courseid;
Cannot use non GROUP BY column 'departmentid' in query results without an aggregate function
Please help me, where I am going wrong.
you have to use group by as you used aggregate funtion
select d.departmentid, d.name,
count(distinct(sg.personid))
from department d inner join course c on c.departmentid = d.departmentid inner join student_grade sg on sg.courseid = c.courseid;
group by d.departmentid, d.name
Use this:-
select d.departmentid, d.name, count(distinct(sg.personid)) from department d inner join course c on c.departmentid = d.departmentid inner join student_grade sg on sg.courseid = c.courseid group by d.departmentid, d.name;

Getting required rows in mysql using GROUPBY

I have a table named travel which is as follows:
+-------+-----+----------+
| Name | Day | Distance |
+-------+-----+----------+
| Ravi | 1 | 10 |
| Ravi | 2 | 21 |
| Ravi | 3 | 23 |
| Hari | 1 | 12 |
| Hari | 2 | 32 |
| Kiran | 1 | 12 |
| Kiran | 2 | 32 |
| Kiran | 3 | 21 |
| Kiran | 4 | 43 |
+-------+-----+----------+
using group in sql for this table as
select name, day, distance, sum(distance) as total_dist
from travel
group by name;
I get the result as follows
+-------+-----+----------+------------+
| Name | Day | Distance | total_dist |
+-------+-----+----------+------------+
| Ravi | 1 | 10 | 54 |
| Hari | 1 | 12 | 44 |
| Kiran | 1 | 12 | 108 |
+-------+-----+----------+------------+
That is mysql gives the top row by default for columns which are not aggregated. But I would prefer to get all the rows with aggregated values or rows with bottom row in the group like as follows:
+-------+-----+----------+------------+
| Name | Day | Distance | total_dist |
+-------+-----+----------+------------+
| Ravi | 3 | 23 | 54 |
| Hari | 2 | 32 | 44 |
| Kiran | 4 | 43 | 108 |
+-------+-----+----------+------------+
or either in this way:
+-------+-----+----------+------------+
| Name | Day | Distance | total_dist |
+-------+-----+----------+------------+
| Ravi | 1 | 10 | 54 |
| Ravi | 2 | 21 | 54 |
| Ravi | 3 | 23 | 54 |
| Hari | 1 | 12 | 44 |
| Hari | 2 | 32 | 44 |
| Kiran | 1 | 12 | 108 |
| Kiran | 2 | 32 | 108 |
| Kiran | 3 | 21 | 108 |
| Kiran | 4 | 43 | 108 |
+-------+-----+----------+------------+
I am new to using mysql and confused with using group by, please guide me if any of the above two results are possible to get.
To get the entire list with the sum for each group, you can use a subquery and join the total back to the original table:
select t.name, t.day, t.distance, t2.total_dist
from travel t
join (select name, sum(distance) total_dist
from travel
group by name) t2 on t.name = t2.name
SQL Fiddle Demo
Or to get the max day per group, just include max(day) in the subquery:
select t.name, t.day, t.distance, t2.total_dist
from travel t
join (select name, sum(distance) total_dist, max(day) max_day
from travel
group by name) t2 on t.name = t2.name and t.day = t2.max_day
More Fiddle
it will work for you
select name,max(day),max(distance)
from tablename
group by name
and other case
select name,max(day),max(distance),(select sum(distance) from table) as total_dist
from tablename
group by name

join with a group by?

i have a table called rc_language_type_table with:
id language
1 english
2 Xhosa
3 afrikaans
etc
then i have a table rc_language_type_assoc_table with:
profile_id | language_type_id |
+------------+------------------+
| 3 | 1 |
| 13 | 1 |
| 15 | 1 |
| 16 | 1 |
where i have profiles and each profile is connected to a language id in a 1 to many
so then i did:
select *,count(*) from rc_language_type_assoc_table group by language_type_id;
+------------+------------------+----------+
| profile_id | language_type_id | count(*) |
+------------+------------------+----------+
| 3 | 1 | 96 |
| 3 | 2 | 19 |
| 3 | 3 | 18 |
| 64 | 4 | 51 |
| 94 | 5 | 10 |
| 37 | 6 | 26 |
| 3 | 7 | 21 |
| 3 | 8 | 4 |
| 3 | 9 | 6 |
| 88 | 10 | 4 |
| 3 | 11 | 3 |
+------------+------------------+----------+
what i want now is: instead having the language_type_id i want to display the actual language...how would i do this please???
i tried:
select *, count(*)
from rc_language_type_assoc_table, rc_language_type_table
group by language_type_id
where rc_language_type_assoc_table.language_type_id = rc_language_type_table.id;
but i get a syntax error...
please help??
thank you
GROUP BY should be "after" the WHERE statement and not before
select *, count(*)
from rc_language_type_assoc_table, rc_language_type_table
where rc_language_type_assoc_table.language_type_id = rc_language_type_table.id
group by language_type_id ;