Assign Text (Label) to values in SQL Query Result - php

All data in my tables are in numeric format. For example,
class sex subject medium
5 1 1 1
1 1 1 2
4 3 3 1
5 1 3 2
1 1 3 1
3 1 2 2
1 2 2 2
Other Table contains Description to each value for each table. For Example,
Table Column value text
Student class 1 1ST
Student class 2 2ND
Student class 3 3RD
Student class 4 4TH
Student class 5 5TH
Student sex 1 male
Student sex 2 female
Student sex 3 creator
Student medium 1 English
Student medium 2 Hindi
I am trying to provide a text (label to each column when user queries). I have been able to do it for one column. For example,
SELECT class, sex, avg(total_marks) as total
FROM (
SELECT value, text as sex
FROM Label
WHERE table_name = 'Student' and
Column = 'class' or
) AS Label INNER JOIN Student on value = class
GROUP BY class, sex
This results in
class sex total
1ST 1 64.80
1ST 2 59.66
2ND 1 78.96
2ND 2 96.97
3RD 1 52.67
3RD 2 81.77
4TH 1 61.99
4TH 2 99.78
5TH 1 72.90
5TH 2 70.59
Instead of 1 / 2 /3 in sex I want to show male, female and creator. Select and Group by columns can be more than 4 at times. I am using MySql and PHP Laravel to do this.

I guess I found the answer. Any suggestions are most welcome.
SELECT T1.class, T2.sex, SUM(weight)
FROM household_characteristics
INNER JOIN (SELECT value_label_name as code, value_label_value FROM labels
WHERE table_name = 'Student' and columns = 'class' ) AS T1 ON T1.code = Student.class
INNER JOIN (SELECT value_label_name as code, value_label_value FROM labels
WHERE table_name = 'Student' and columns = 'sex' ) AS T2 ON T2.code = Student.sex
GROUP BY class, sex

Related

Joining two mysql tables to get a true/false column

I'm working with PHP and MySql. I'm trying to find a way to select a number of movies from a mysql table, but apart from the movies table I have a watchlist table that stores a userID and the movieID of the movies he/she has added to his/her watchlist:
id userID movieID
=====================================
1 1 3
2 1 5
3 1 7
4 2 3
5 2 2
6 3 2
The movies table looks something like this
movieID title duration
=============================
1 tit1 34:43
2 tit2 35:43
3 tit3 24:43
4 tit4 34:13
5 tit5 11:43
6 tit6 22:43
7 tit7 33:43
The result I'm after is (for example for the user with ID 1):
movieID title duration added
=======================================
1 tit1 34:43 false
2 tit2 35:43 false
3 tit3 24:43 true
4 tit4 34:13 false
5 tit5 11:43 true
6 tit6 22:43 false
7 tit7 33:43 true
Is there a way to join both the movies and the watchlist table to produce the desired result?
Thanks.
You can get the required output by using a LEFT JOIN and then checking for a NULL in the join table.
SELECT m.*, IF(w.id IS NULL, 0, 1) AS added
FROM movies m
LEFT JOIN watchlist w ON (m.movieID = w.movieID AND w.userID = 1)
GROUP BY m.movieID

Calculation table2 data and updated to table1 LAravel

Table1
id name calculated_rating
1 xyz 2
2 abc 4.5
3 zzz 1
4 ddd 3
5 eee 2
Table2
id f_id rating
1 1 3
2 2 4
3 2 5
4 3 1
5 1 2
6 4 3
7 5 2
I have two tables one is Table1 and other is Table2
In table2 f_id that is foreign_key Table1 id is primary_key
Now Table2 has rating I want to added the calculate average rating in Table1 whenever Table2 increase rating it calculate average and update into Table1 calculated_rating field
How to achieve this in laravel
Whenever you add records to table2 like following you need to update current_rating value in table1.
Example,
id = 7
f_id = 5
rating = 2
You can create model for each table such as Table1 and Table2 using artisan command.
Now, Insert records like this:
$table2 = new Table2();
$table2->f_id = 5;
$table2->rating = 2;
$table2->save();
Now update new rating like this:
Table1::where('id',$table2->f_id)->update(['calculated_rating'=> Table2::where('f_id',$table2->f_id)->avg('rating')]);
Hope, this might help you.

Remove Duplicate Row Except The Last One

I have this table on MySql:
Table1
ID CODE USER NUMBER ADJUSTMENT ADJUST_DATE
1 abc Frank 10245 1 2015/04/20
2 def Jonathan 25410 0 2015/04/21
3 ghi Karen 55214 3 2015/05/05
4 abc Frank 10245 2 2015/04/21
5 abc Frank 10245 4 2015/04/22
I would like to remove the duplicated data and leave the last entry by date:
ID CODE USER NUMBER ADJUSTMENT ADJUS_DATE
2 def Jonathan 25410 0 2015/04/21
3 ghi Karen 55214 3 2015/05/05
5 abc Frank 10245 4 2015/04/22
CODE, USER, NUMBER, ADJUSTMENT, ADJUS_DATE are 'Unique'
I need to create a temporary table with the result because I need all the records.
Generate a subset of the max date grouped by like values in columns and join back to the base set...
SELECT A.ID, A.Code, A.user, A.Number, A.Adjustment, A.Adjust_date
FROM table1 A
INNER JOIN (SELECT Code, User, Number, max(adjust_date) mDate
FROM table1 group by Code, User, Number) B
on A.code = B.code
and A.user = B.User
and A.Number = B.Number
and A.Adjust_date = B.mdate

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

SUM acorrding to another column

I need to know if there is a possible way doing this with out subquery..
Here is my table structure:
id-name-father_id
1 joe 0
2 mark 0
3 muller 0
4 miki 2
5 timi 2
6 moses 2
7 david 1
8 momo 0
9 daniel 0
10 ermi 3
My table logic is
0 means he is not a child of some one
1+ mean that he is son of man in that row.
Note: if some one have a child, he still
will have 0 in father id (it's mean there is not grand-fathers in my table)
My query is :
SELECT id, name, count(id=father_id) as sons
WHERE father_id = 0
What I want to get is a list of non-children (father_id=0) and sum
the childrens it has.
Is there a way to get the results without a subquery?
This should do it (MySQL):
SELECT `parents`.`id`, `parents`.`name`, COUNT(`children`.*) AS sons
FROM `people` AS parents
LEFT JOIN `people` AS children ON `parents`.`id` = `children`.`father_id`
WHERE `parents`.`father_id` = 0
GROUP BY `parents`.`id`
According to Gary we need to add name to GROUP BY in other SQL databases:
SELECT `parents`.`id`, `parents`.`name`, COUNT(`children`.*) AS sons
FROM `people` AS parents
LEFT JOIN `people` AS children ON `parents`.`id` = `children`.`father_id`
WHERE `parents`.`father_id` = 0
GROUP BY `parents`.`id`, `parents`.`name`
We are joing the table with itself here. So we join all parents with their children.
This will lead to a result like that:
parents.id parents.name children.id children.name
1 joe 7 david
2 mark 4 miki
2 mark 5 timi
2 mark 6 moses
3 muller 10 ermi
8 momo - - # left join allows this line
9 daniel - -
But now we have each parent several times. So we are GROUP'ing the whole thing over the parent’s id, which will result in the following:
parents.id parents.name COUNT(children.*)
1 joe 1
2 mark 3
3 muller 1
8 momo 0
9 daniel 0
You should be able to do it without any joins or sub-queries as follows:
select case father_id when 0 then id else father_id end id,
max(case father_id when 0 then name end) name,
sum(sign(father_id)) sons
from table
group by case father_id when 0 then id else father_id

Categories