FInding the columns count with mutliple columns - mysql

I have a table as follows:
ID PID TID CID DID IsTrue
1 43 1 2 621 0
2 43 1 2 621 1
3 45 2 3 621 1
4 46 2 3 621 0
and I need to find the count of PID,CID and with the where condition DID=621 along with the other fields.
how can I do that?
Thanks.

Try this
Select count(pid) from table where did = 121 group by pid.

Related

Query to get rows with two consecutive zeros in a column

I have a data set:
sid name sub marks subid
11 kittu eng 55 1
11 kittu math 0 2
11 kittu sci 0 3
12 bunnu eng 0 1
12 bannu math 44 2
12 bannu sci 0 3
13 siva eng 0 1
13 siva math 0 2
13 siva sci 88 3
and I want to get output like this (which have consecutive zero marks):
11 kittu
13 siva
SELECT a row from T WHERE exists a row another with same sid and a lower subid sequentially and both rows have zero marks.
Then Do a DISTINCT just in case there are more than qualifying condition so you don't get duplicates.
SELECT DISTINCT sid,name
FROM t
WHERE EXISTS (SELECT 1 FROM T T2
WHERE T2.marks = 0
AND t.marks = 0
AND T2.sid = T.sid
AND T2.subid = t.subid + 1)
sqlfiddle

Use avg() and joins mysql

Say I have a table called students
idStudent Name
1 Billy
2 Mariah
3 Chris
4 Mark
5 Sarah
and another table called tests
idTest score student_idstudent
1 50 1
2 100 1
3 90 2
4 100 3
5 45 4
is it possible to use a combination of a join and avg() to get a result like
idStudent avg_test
1 75
2 90
3 100
4 45
5 0
SELECT s.idStudent,
AVG(COALESCE(t.score, 0)) AS avg_test
FROM students s
LEFT JOIN tests t
ON s.idStudent = t.student_idStudent
GROUP BY s.idStudent

Pivot tables in mysql columns

I have table localbanya ,I wanted to do pivoting of its column, Here is my table structure
Billingid prod_id qty
1 11 2
1 22 5
2 11 1
2 22 3
2 33 4
3 11 2
3 22 1
I'm expecting something like this
Billinid 11 22 33 Total
1 2 5 0 2+5+0
2 1 3 4 1+3+4
3 2 1 0 2+1+0
I have tried this but unable to get the desired result
SELECT billingid,GROUP_CONCAT(qty)
FROM localbanya
GROUP BY billingid;
please help, Thanks in advance

Update value in a column based on another column in the same table in MYSQL

I have a table that is having 3 columns
vid - auto increment column
video_id - containing numbers
a_id - containing junk numbers
The table looks like below.
Vid Video_id a_id
101 1 3
102 1 3
103 5 3
104 5 3
105 5 3
106 11 3
107 11 3
108 11 3
109 11 3
110 11 3
I want to update a_id column values based on video_id values. Values in a_id should be updated as below.ex: If there are five 11 digit in video_id then the value in a_id should be updated 1 through 5.
Vid Video_id a_id
101 1 1
102 1 2
103 5 1
104 5 2
105 5 3
106 11 1
107 11 2
108 11 3
109 11 4
110 11 5
You can use user defined variables to give rank for each video group and then join with your real table by your auto increment column and update a_id accordingly
update t
join (
SELECT
Vid,
#r:= CASE WHEN Video_id = #g THEN #r+1 ELSE #r:=1 END a_id
,#g:=Video_id
FROM t,(SELECT #r:=0,#g:=0) t1
ORDER BY Video_id
) t1
on(t.Vid =t1.Vid)
set t.a_id = t1.a_id
Demo

SQL many-to-many count sum

I have this table:
id_user id_user2
1 54
1 53
1 53
1 54
1 54
1 55
2 23
2 23
2 20
2 21
2 25
2 25
And i would like to count, how many each of id_user have relationship with id_user2. Output should be:
id_user id_user2 result
1 54 3
1 53 2
1 55 1
2 23 2
2 20 1
2 21 1
2 25 2
You have to use group by clause
select id_user,id_user2, count(1) as result
from userstab
group by id_user,id_user2
try this query
select id_user,id_user2,count(id_user2) as result
from TABLE_NAME group by id_user2