How to get the count of rows from a table in cakephp - mysql

I have two tables:
id category status
1 test 1
2 test1 1
3 test2 1
This is the group_master table.
groupid groupname groupmaster
1 yy 1
2 xx 1
3 yyyy 1
4 xrx 1
5 yy 2
6 xx 2
7 yyyy 2
8 xfgdrx 3
This is the membergroup table.
The group_master.id is same as in membergroup.groupmaster.That menas i want to get each row from first table and also want to get the count from second table
That means:
id category status Count
1 test 1 4
2 test1 1 3
3 test2 1 1
This is the result i want to get.
How can i do this in Cakephp with pagination ?

try this:
You need to JOIN both tables and do a GROUP BY
SELECT g.id,g.category,g.status,count(*) as Count
FROM group_master g
JOIN membergroup m
ON g.id=m.groupmaster
GROUP BY g.id,g.category,g.status
SQL Fiddle Demo

Related

query to get distinct data from child table

please provide query to getting my result.
i have two tables as follows.
price_band
id club_id name price
1 6 test 2.3
2 6 test1 3.3
price_band_seat
id price_band_id row seat block_id
1 1 a 1 1
2 1 a 2 1
3 1 b 1 2
4 2 b 2 2
and result that i want
Price block_id price_band_id row
2.3 1 1 a
2.3 2 1 b
3.3 2 2 b
query exclude that raw which block_id and price_band_id are same . in where clues you have to take club_id=6
Please try this.
SELECT
DISTINCT
A.Price,B.block_id,B.price_band_id,B.row
FROM
price_band A
INNER JOIN price_band_seat B
ON A.id = B.price_band_id
WHERE A.club_id = 6

MS Access Sum and Averaging Columns with Same Information as New Column

So I have the following chart here where I have columns:
a b c d
1 1 1 3
1 1 1 4
1 1 1 5
2 2 2 1
2 2 2 3
2 2 2 3
3 3 3 4
3 3 3 5
3 3 3 6
What I want to do is add and average column d where columns a,b,c contain the same values. How would I go about doing this?
I imagine it would be something like
Select SUM(Table.d) where a = b AND b = c AS e
Try this:
Select SUM(Table.d), AVG(Table.d) from Table Group by Table.a, Table.b, Table.c

MySQL: Find "same" Data

I am currently in the need to find entrys matching the same pattern in a connection table.
The Table looks like
id job_id data1 ext_id
-- ------ ----- -----
1 15 1 3
2 15 2 7
3 1 1 5
4 1 2 4
5 5 1 3
6 5 2 7
so my basic information is the data of job_id 15
id job_id data1 ext_id
-- ------ ----- -----
1 15 1 3
2 15 2 7
I want to find job_id 5 because the data in ext_id and data1 is the same as in job 15. the data of job_id 1 differs, so I don't want to find that.
Any idea on how to do it?
I believe you want this:
select *
from your_table
group by data1,
ext_id
having count(*) > 1
This post explains it:
How to find duplicates in 2 columns not 1
EDIT
I believe this should return all rows that have mathcing data1 and ext_id values
select * from table t1
INNER JOIN table t2 ON t1.data1=t2.data1 and t1.ext_id=t2.ext_id

SQL - selecting count of different value

Assume I have table count and the column is (nilai, id_courses, id_lecturer)
nilai id_courses id_lecturer
----- ---------- -----------
2 1 1
2 1 1
2 1 1
3 1 1
3 1 1
1 2 1
1 2 1
5 2 1
5 2 1
then I want to create view like this :
nilai id_courses id_lecturer count
----- ---------- ----------- -----
2 1 1 3
3 1 1 2
1 2 1 2
5 2 1 2
how to do that in SQL syntax?
I just know how to count 1 value with this code
SELECT COUNT( nilai ) , id_courses, id_lecturer FROM count where nilai=1
I've read this post but its to complex, so I don't know how it's work
You need to count all distinct entries by grouping them. The query
SELECT nilai, id_courses, id_lecturer, COUNT(*) AS count
FROM count GROUP BY nilai, id_courses, id_lecturer
should exactly return the table you posted.

MySql subqueries and max or group by?

I have this table:
ID STUDENT CLASS QUESTION ANSWER TIME
1 1 1 1 c 12:30
2 1 1 1 d 12:36
3 1 1 2 a 12:38
4 2 1 1 b 11:24
5 2 1 1 c 11:26
6 2 1 3 d 11:35
7 2 3 3 b 11:24
I'm trying to write a query that does this:
For each STUDENT in a specific CLASS select the most recent ANSWER for each QUESTION.
So, choosing class "1" would return:
ID STUDENT CLASS QUESTION ANSWER TIME
2 1 1 1 d 12:36
3 1 1 2 a 12:38
5 2 1 1 c 11:26
6 2 1 3 d 11:35
I've tried various combinations of subqueries, joins, and grouping, but nothing is working. Any ideas?
You can use a sub-query to get most recent ANSWER per QUESTION, Then use this as a derived table and join back to the original table:
SELECT m.*
FROM mytable AS m
INNER JOIN (
SELECT STUDENT, QUESTION, MAX(`TIME`) AS mTime
FROM mytable
WHERE CLASS = 1
GROUP BY STUDENT, QUESTION
) AS d ON m.STUDENT = d.STUDENT AND m.QUESTION = d.QUESTION AND m.`TIME` = d.mTime
WHERE m.CLASS = 1
Demo here