how to generate students mark ledger from 3 tables- Student,Mark,Subject - php

I have four tables
1.student-it contains id name etc
2.marks - it contains markid,student_id,batch,class,section,subject_id,exam_id,obt_mark
3.subject table - it contains sub_id,sub_name
4. exam_type- examtype_id, exam_name
i need mark ledger for batch-2014,class=5,section=1,exam_id=3 as shown below-
NAME ROLL NO SUB1 SUB2 SUB3 TOTAL RESULT POSITION RANK
RAM 0054 65 54 64 183 PASS FIRST 2
HAri 0054 65 54 65 184 PASS FIRST 1
gopal 0054 65 50 65 180 PASS FIRST 3
saroj 0054 65 44 65 174 PASS FIRST 4
Thanks in advance .
Here i have sql query
SELECT `marklists`.`mrk_sub_id1`, `marklists`.`mrk_marks`, `marklists`.`mrk_practical`,
`marklists`.`mrk_exam_type`, `students`.`id` as st_id, `students`.`st_roll`,
`students`.`st_name`, `students`.`batch`, `students`.`st_class`,
`students`.`st_section`, `courses`.`id`, `classes`.`class_name` as class_name,
`courses`.`sb_name` as subject_name, `courses`.`sb_fullmark` as full_marks,
`courses`.`sb_passmark` as pass_marks FROM (`marklists`)
LEFT JOIN `classes` ON `marklists`.`mrk_class` = `classes`.`id`
LEFT JOIN `students` ON `marklists`.`mrk_student_id`=`students`.`id`
LEFT JOIN `courses` ON `marklists`.`mrk_sub_id`=`courses`.`id`
WHERE `marklists`.`mrk_exam_type` = '3' AND `marklists`.`mrk_batch` = '3'
AND `marklists`.`mrk_class` = '1' AND `marklists`.`mrk_section` = '1'
and I need ledger like above any help??

Join the tables based on common key and for the titles which you need as subject you can use pivot operator to convert rows to column and use where clause for rest of the conditions
hope this helps.

Related

How to make sum of two rows in a table?

The table has the values like,
group_name group_id parent_group_id amount
One 69 10 1000
Two 70 11 500
Three 71 69 700
Here I need to make sum of the values in amount column if the group id = 69 matches with the parent_group_id = 69 for which I have tried with
SUM(CASE WHEN fee_groups.groups_id = fee_groups.parent_fee_groups_id
THEN amount
ELSE 0
END) AS total
But above line doing nothing for me. If I am wrong with it then kindly help me to achieve the following result,
group_name group_id parent_group_id amount
One 69 10 1700
Two 70 11 500
Here the group_id and parent_group_id make a relationship, if there is a match from any of the parent_group_id with group_id then both are comes under single group and hence I need to sum both the values.
It doesn't make sense to me to include group_id and parent_group_id given what you want to do. You also don't explain what happens to the "71".
select min(group_name) as group_name,
sum(amount)
from t
group by (case when 69 not in (group_id, parent_group_id) then group_name end);
This will combine the groups that share "69" and keep the others separate.
Try this solution:
SELECT t1.group_name, t1.group_id, t1.parent_group_id,
(t1.amount + IFNULL(t2.amount,0)) Amount
FROM Table1 t1
LEFT JOIN Table1 t2
ON t1.group_id = t2.parent_group_id
OR t2.group_id = t1.parent_group_id
WHERE t1.group_name < t2.group_name OR t2.group_name IS NULL
GROUP BY t1.group_name, t1.group_id, t1.parent_group_id
OUTPUT:
group_name group_id parent_group_id Amount
One 69 10 1700
Two 70 11 500
Try this
SELECT
group_name,group_id,parent_group_id,
(CASE WHEN group_id = 69 THEN
(amount+(SELECT amount FROM table1 WHERE parent_group_id = 69))
ELSE amount END) as new_amount
FROM Table1 WHERE parent_group_id !=69;
do the execute below query or this link
sqlfiddle
select t.group_name,t.group_id,t.parent_group_id,
(case when t.group_id = t1.parent_group_id then t1.amount + t.amount else t.amount end)
from t
left join t as t1 on t.group_id = t1.parent_group_id
where t.group_id <>71

count number of rows associated with their id in another table

Hi We have 3 Table of a music which is something like this in MySql :
1st Table :
the first table is for playlist table where music playlist is exist.
playlistId playlistTitle categoryId
1 hello 0
2 wow 0
3 wi-fi 0
4 awesome 0
5 sixer 1
6 four 1
7 boundary 2
2nd Table :
2nd table is for songRelation table where every playlist is associated with thier song
playlistId songId
1 4
1 3
1 43
1 57
1 98
2 56
2 67
2 90
2 78
3 98
3 78
3 89
43 90
3rd Table : the 3rd table is for song table where song detail exist
songId songTitle
4 hello
3 real hero
43 singalone
57 awesom
98 really
78 sakaka
98 shikwa
89 moha
90 hello2
67 Sneh
actually i am fetching the result something like this:
playlistId songId categoryId songTitle
1 4 0 hello
1 3 0 real hero
2 56 0 singalone
2 67 0 Sneh
3 78 0 sakaka
3 98 0 Shikwa
where the every playlistId will be with their first 2songIdand with theircategoryIdand also withsongTitle`.
but i want to count the total song with every playlistId
after getting the total song result i want will be something like this :
playlistId songId categoryId songTitle totalSong
1 4 0 hello 5
1 3 0 real hero 5
2 56 0 singalone 4
2 67 0 Sneh 4
3 78 0 sakaka 3
3 98 0 Shikwa 3
here is the jsfiddle Demo where query is without totalSong http://sqlfiddle.com/#!9/7eda7/5
What subquery will be added to get the above desired result.
To get exactly the result you asked, use this:
select p.playlistId,
s.songId,
p.categoryId,
s.songTitle,
(select count(*) from songRelation where playlistId = p.playlistId) totalSong
from playlist p
inner join songRelation r on p.playlistId = r.playlistId
inner join song s on r.songId = s.songId
Using a group by on the main query would merge the detailed song data, forcing you to run two queries: one for details (first 4 fields) and a second query, to recover the totals (last column). Using this solution, you get all detailed data and totals, the sub-query will recover the count of songs for each playlist, the way you asked.
UPDATE:
This way, suggested by rlanvin, should make the query faster, because instead on computing the subquery for each row, it gets computed only once, and then is joined to the main query. The result is the same:
select p.playlistId,
s.songId,
p.categoryId,
s.songTitle,
r1.totalSong
from playlist p
inner join songRelation r on p.playlistId = r.playlistId
inner join song s on r.songId = s.songId
inner join (SELECT playlistid, COUNT(songId) as totalSong from songRelation group by playlistid) r1 on p.playlistId = r1.playlistId
I have added this query to ur SQLFIDDLE.
SELECT p.playlistId, s.songId, p.categoryId, s.songTitle,
(select count(sr1.songId) from songRelation sr1
where sr1.playlistid=p.playlistid
group by sr1.playlistId) as total,
#r := IF (#pid = p.playlistId,
IF (#pid := p.playlistId, #r+1, #r+1),
IF (#pid := p.playlistId, 1, 1)) AS rn
FROM playlist AS p
CROSS JOIN (SELECT #r:=0, #pid:=0) AS vars
INNER JOIN songRelation AS sr ON p.playlistId = sr.playlistId
INNER JOIN song AS s ON sr.songid = s.songid
ORDER BY p.playlistId, s.songId ) AS t
WHERE t.rn <= 2
It is giving the required output. Check the Demo Here
Using Group Functions you can do this:
SELECT `playlistid`, COUNT(`songid`)
FROM `playlist`
GROUP BY `playlistid`

Issue while copying tables on PHP SQL

All id are auto_increment. I will try explain my issue understandable. I just want copy 2 tables to another 2 tables. Here my tables at below:
table1
id number
10 100
11 102
12 105
13 106
table2
id number_id subnumber
52 10 10
53 11 15
54 13 40
You can see there is subnumber of some numbers. For example, WHERE id =11 from table1 has a subnumber on table2 and it is equal to 15. Now I should copy the table1 to the table : copy_table1
$sql1 = mysql_query('INSERT INTO
copy_table1 (copy_number)
SELECT
number
FROM
table1');
And the results of copy_table1 are:
copy_table1
id copy_number
100 100
101 102
102 105
103 106
Then I should copy table2 to another table copy_table2:
$sql2 = mysql_query('INSERT INTO
copy_table2 (copy_number_id, copy_subnumber)
SELECT
number_id, copy_subnumber
FROM
table2');
And the results of copy_table2 are:
id copy_number_id copy_subnumber
60 10 10
61 11 15
62 13 40
So, lets check the tables: copy_table1 and copy_table2.
You see, ID from copy_table1 is not equal and suitable to copy_number_id FROM copy_table2
And it brings me problem. How can I do that after copying tables id and copy_number_id will be suitable to each other?
$sql2=mysql_query('INSERT INTO
copy_table2 (copy_number_id, copy_subnumber)
SELECT DISTINCT
cp1.id, t2.subnumber
FROM
copy_table1 AS cp1
CROSS JOIN
table1 AS t1
USING (number)
INNER JOIN
table2 AS t2
ON
t1.id = t2.number_id ');

Query and table - MAX and Join

Still very new to all of this so bear with me.
Have 3 tables
table 1: member
Mem_index, Mem_name
1 joe
2 Mark
Table 2: Course
Course_index, Course_Name
1 Math
2 Reading
Table 3 : data
Data index,Member,Course,Score
1 1 1 85
2 1 2 75
3 2 1 95
4 1 2 65
SO what I would like to do is create a table:
Do a query and gather all of the courses, find the max score for each course and attribute the member name to it.
Table result should look like:
Course, Max score,name
Math 95 Mark
Reading 75 Mark
I can do the query individually but unsure of how to loop it and then propogate the data into the table.
How about this query for SQL?
SELECT c.course_name, MAX( d.score ), m.mem_name
FROM members m
JOIN data d on m.mem_id = d.member
JOIN course c on c.course_id = d.course
GROUP BY d.course
ORDER BY d.score, m.mem_name, c.course_name
Not sure if the field names match up but you get the idea - tested this in sql with some dummy data.
Data
Index Member Course Score
1 1 1 60
1 1 1 85
Course
course_id course_name
1 Math
2 English
3 Science
Members
mem_id mem_name
1 Mark
2 James
You will get the following
Course Name Score Member
Math 85 Mark
Try this query :
SELECT c.course_Name , MAX(d.score),m.mem_name
FROM data d
JOIN course c ON d.course=c.course_index
JOIN members m ON m.mem_index = d.member
GROUP BY d.course
ORDER by MAX(d.score) DESC

mysql, php, how to compare numbers

i have a database: database with a table: table and some fields:
id fname dphone count_pic dup_id
6055903 Karla 5126xxx798 1 57
6173767 Aaliyah 4082xxx534 4 39
5611411 Aaliyah 4082xxx534 15 39
5611211 Aaliyah 4082xxx534 18 39
4234798 Abby 3057xxx974 31 16
6166691 Walter 6178xxx280 1 74
3375576 Walter 6178xxx280 17 74
what i am trying to do is to select the fields that have the smallest count_pic and the ones that have the bigger count_pic and that have the same dup_id
any ideas how to do this in mysql?
thanks.
With this query you'll select the smallest and the biggest values of count_pic for every dup_id
SELECT MIN(count_pic) AS minpic,
MAX(count_pic) AS maxpic,
dup_id
FROM `table`
GROUP BY dup_id
If you also need corresponding rows, then you could use something like
SELECT *
FROM `table` t1
INNER JOIN (SELECT MIN(count_pic) AS minpic,
MAX(count_pic) AS maxpic,
dup_id
FROM `table`
GROUP BY dup_id) t2 ON t1.dup_id = t2.dup_id
AND (t1.count_pic = minpic
OR t1.count_pic = maxpic)

Categories