I have two mysql tables
suppose table one name 'marks'
no A B C D
1 10 05 01 04
2 08 07 10 05
3 09 05 07 10
4 07 05 04 10
5 04 07 06 09
6 05 09 07 07
7 09 05 10 06
8 09 06 06 08
9 08 06 10 07
10 08 07 04 06
suppose table two name 'results'
in second table I want to put total marks and average marks based on above table.(import data from 'marks' table,process it and save it in 'results' table)
So once it filled it must be like this.
I want add column A,B,C,D in 'marks' table and put total value in column 'Total' in table 'results' and average by dividing 'Total' column by 4.
no Total Average
1 20 5.00
2 30 7.50
3 31 7.75
4 26 6.50
5 26 6.50
6 28 7.00
7 30 7.50
8 29 7.25
9 31 7.75
10 25 6.25
So how can I fill the 'result' table using mysql query?
Is it possible to do in mysql?
Thank you
Try something like:
INSERT INTO result (no, total, average)
SELECT no, A+B+C+D, (A+B+C+D)/4
FROM marks
Related
i have 2 tables : dt_user and dt_invoice.
**dt_members :**
id firstname
3 Salim
5 Sara
8 Julie
**dt_invoice**
user_id amount_ht period month year
3 4950 04 2018 04 2018
3 7200 10 2018 10 2018
8 11000 10 2018 10 2018
8 5500 11 2018 11 2018
3 6750 11 2018 11 2018
3 8700 12 2018 12 2018
3 8800 01 2019 01 2019
8 7500 01 2019 01 2019
3 4950 02 2019 02 2019
3 7550 03 2019 03 2019
I want to create a query joining the two table, but i want to show each user_id for PERIOD that there is in table dt_invoice.
**Expected results :**
user_id amount_ht period month year
3 4950 04 2018 04 2018
5 0 04 2018 04 2018 //non-existent record in dt_invoice
8 0 04 2018 04 2018 //non-existent record in dt_invoice
3 7200 10 2018 10 2018
5 0 10 2018 10 2018 //non-existent record in dt_invoice
8 11000 10 2018 10 2018
8 5500 11 2018 11 2018
5 0 11 2018 11 2018 //etc ...
3 6750 11 2018 11 2018
3 8700 12 2018 12 2018
5 0 12 2018 12 2018
8 0 12 2018 12 2018
3 8800 01 2019 01 2019
5 0 01 2019 01 2019
8 7500 01 2019 01 2019
3 4950 02 2019 02 2019
5 0 02 2019 02 2018
8 0 02 2019 02 2018
3 7550 03 2019 03 2019
5 0 03 2019 03 2018
8 0 03 2019 03 2018
Thanks in advance for your help, i'm totally stuck ..
SQL datas available here : https://rextester.com/live/LBSEY76360
also in sqlfiddle : http://sqlfiddle.com/#!9/728af3/1
Use a cross join to generate the rows and left join to bring in the values:
select m.user_id, p.period, p.month, p.year,
coalesce(t.amount_ht, 0) as amount_ht
from dt_members m cross join
(select distinct period, month, year from dt_invoice) p left join
dt_invoice t
on t.user_id = m.id and t.period = p.period;
Maybe this would help.
SELECT user_id, amount_ht, period, month, year
FROM dt_invoice
LEFT JOIN dt_members ON user_id = id
How can i calculate Month on Month Growth Calculation in MySQL 5.7. There is no lag function in MySQL 5.7.
My DB contains is as follow
Date State Value
(dd/mm/yy)
1/1/2017 01 25
1/1/2017 02 35
1/2/2017 01 45
1/2/2017 02 58
1/3/2017 01 68
1/3/2017 02 78
I need the output as below
Date State Value MoM
1/3/2017 01 68 XX%
1/2/2017 01 45 XX%
1/1/2017 01 25
1/3/2017 02 78 XX%
1/2/2017 02 58 XX%
1/2/2017 02 35
Thank you
N Rajkumar
I have a database table WysTeacherattendance -table fields are(id t_adate t_amonth t_ayear t_attendance )
id t_adate t_amonth t_ayear t_attendance
1 15 07 2015 1
2 15 07 2015 1
3 15 07 2015 1
4 15 07 2015 1
5 15 07 2015 1
6 15 07 2015 1
7 15 07 2015 1
8 15 07 2015 1
9 15 07 2015 1
10 15 07 2015 1
i have 10 teachers and iam enter all attendances are 1, insert into database t_attendance is 1 for all 10 rows.
when im enter submit button values go to database and redirect to another page ,before check all t_attendance is 1 or not .
1st condition
if its all 1 go to home page.
id t_adate t_amonth t_ayear t_attendance
1 15 07 2015 1
2 15 07 2015 1
3 15 07 2015 1
4 15 07 2015 0
5 15 07 2015 1
6 15 07 2015 1
7 15 07 2015 0
8 15 07 2015 1
9 15 07 2015 1
10 15 07 2015 1
2nd condition
if any t_attendance value is 0 then go to attendance page.
how to check these 2 conditions.
iam us its controller code
foreach ($teachers as $teacher){
$teacheratt = new WysTeacherattendance();
$teacheratt->t_auserid = $teacher->user_id;
$teacheratt->t_adate=$date_exploded[0];
$teacheratt->t_amonth=$date_exploded[1];
$teacheratt->t_ayear=$date_exploded[2];
$teacheratt->t_attendance=Input::get($teacher->user_id);
$teacheratt ->save();
}
if($users = DB::table('wys_teacherattendances')
->where('t_adate',$date_exploded[0])
->whereIn('t_attendance', array(1))->get())
{return Redirect::route('GetTeachersearchAttendence')
->with('success',' teacher exist');
}
if($users = DB::table('wys_teacherattendances')
->where('t_adate',$date_exploded[0])
->where('t_attendance', '=',0)->get())
{return Redirect::route('AddTeacherabsentReason',$date_exploded[0])
->with('success',' teacher exist');
}
}`
but only work this condition `if($users = DB::table('wys_teacherattendances')
->where('t_adate',$date_exploded[0])
->whereIn('t_attendance', array(1))->get())
{return Redirect::route('GetTeachersearchAttendence')
->with('success',' teacher exist');
}
how solve it??
try this
if($users == DB::table('wys_teacherattendances')
->where('t_adate',$date_exploded[0])
->whereIn('t_attendance', array(1))->get())
{return Redirect::route('GetTeachersearchAttendence')
->with('success',' teacher exist');
}
for compare result, you must use ==, not = (= assigning value, not checked the value)
I think you must change all "=" you have typed with "=="
I only give the idea
You can check the number of that teacher
$teacher_number = "SELECT COUNT(teacher) from teacher"
You can check the number of that attendance
$attendance = "SELECT COUNT(attendance) from attendance where
attendance = 1 and date = yourdate"
if $teacher_number == $attendance
redirect home.page
else if $teacher_number > $attendance
redirect attendance.page
How would I get a sum of sales totals for the current week against the same week last year?
There are two possible scenarios related to how the dates are stored, as below:
Scenario 1
**Sales**
Date Sales
-----------------------
2012-08-10 11040.00
2012-08-09 11500.00
2012-08-08 14060.00
2012-08-07 93000.00
2012-08-06 11200.00
...
2011-08-10 11040.00
2011-08-09 11500.00
2011-08-08 14060.00
2011-08-07 93000.00
2011-08-06 11200.00
Scenario 2
**Sales**
year month day Sales
---------------------------------------------
2012 08 10 11040.00
2012 08 09 11500.00
2012 08 08 14060.00
2012 08 07 23000.00
2012 08 06 11200.00
...
2011 08 10 13040.00
2011 08 09 11500.00
2011 08 08 12060.00
2011 08 07 33000.00
2011 08 06 11250.00
For your first scenario, join against the same table on the WEEKOFYEAR() and one added to last year's YEAR():
SELECT
YEARWEEK(thisyear.Date) AS `YearWeek`
SUM(lastyear.Sales) AS LastYearSales
SUM(thisyear.Sales) AS ThisYearSales
FROM
Sales thisyear
LEFT JOIN Sales lastyear ON
WEEKOFYEAR(thisyear.Date) = WEEKOFYEAR(lastyear.Date)
AND YEAR(thisyear.Date) = (YEAR(lastyear.Date) + 1)
GROUP BY `YearWeek`
The second scenario requires building a date out of the 3 separate values. I think this will work:
SELECT
YEARWEEK(CONCAT_WS('-', thisyear.year, thisyear.month, thisyear.day)) AS `YearWeek`,
SUM(lastyear.Sales) AS LastYearSales,
SUM(thisyear.Sales) AS ThisYearSales
FROM
Sales thisyear
LEFT JOIN Sales lastyear ON
WEEKOFYEAR(CONCAT_WS('-', thisyear.year, thisyear.month, thisyear.day)) = WEEKOFYEAR(CONCAT_WS('-', lastyear.year + 1, lastyear.month, lastyear.day))
GROUP BY `YearWeek`
E.g: DISTINCT file_sn and got the merge[union,unique] all_files result:
user_id file_sn all_files[char]
1 1 01 02 05
2 1 02 05
3 1 05 06 07 08
4 1 10 11 12 15
So,I want to merge[union] the column: all_files so I could get the unique all_files data like:
01 02 05 06 07 08 10 11 12 15
Thank you very much~~
[UPDATE]
My query to get the all all_files query:
SELECT file_sn, GROUP_CONCAT(DISTINCT all_files SEPARATOR ' ')
FROM `myTable`
GROUP BY file_sn
It will return:
01 02 05 02 05 05 06 07 08 05 06 07 08