I have a table where I need to count all present to show how many attendance student have
In my table have Following Column
school id
Student id
year
Month
Status
day_1
day_2
day_3
To
day_31
entry goes once in a month. Like
if new month new row create
else update in day_ 1 day_2 day_3 etc.
Here's a different trick. Although it's for MySql 8.0+
Concatinate the day_n fields into a string.
Then remove all the characters that aren't 'P' from that string.
Then the character length of the remaining string will be the total 'P'.
SELECT school_id, student_id, month, year,
CHAR_LENGTH(
REGEXP_REPLACE(
CONCAT_WS('' -- using concat_ws because concat returns null when one of the fields is null
, day_1
, day_2
-- add more day_n here
, day_31),'[^P]+','')) AS present
FROM YourTable
A more conventional approach might look something like this:
student_id,
attendance_date
You can made a query to get the IDs of all students,
$sql2 = "SELECT distinct id from table";
$query = $this->db->query($sql2);
foreach ($query->result() as $innerrow) {
HERE YOU USE THE SYNTAX OF NEXT CODE-BLOCK
}
Inside the foreach loop you can use the id with $row->id to generate another sql query
with an where clause.
$sql = "SELECT table.* from table where id = ".$row->id;
$query = $this->db->query($sql);
foreach ($query->result() as $row) {
print your data
}
write query to find the attendance with present like
select (if(day_1 in('P'),1,0)+if(day_2 in('P'),1,0)+if(day_3 in('P'),1,0)+if(day_4 in('P'),1,0)+if(day_5 in('P'),1,0)+if(day_6 in('P'),1,0) + if(day_7 in('P'),1,0)+if(day_8 in('P'),1,0)+if(day_9 in('P'),1,0)+....+if(day_n in('P'),1,0)) as day_present from tbl_name
But the if condition for all 31 days or 30 days as per months days
just try it once in your mysql query editor You will get the exact result..
In codeigniter
$result = $this->db->select("(if(day_1 in('P'),1,0)+if(day_2 in('P'),1,0)+if(day_3 in('P'),1,0)+if(day_4 in('P'),1,0)+........+if(day_n in('P'),1,0)) as present, school_id, student_id,year, month, status");
$this->db->get('table_name');
Related
I'm trying to perform a field update on mysql with a MAX() value getting other columns of the same table .
For instance, I've this table:
id starting_date activity_1 activity_2 activity_3
1 0000-00-00 10 5 12
I'm trying this query (it doesn't work):
$today="2022-07-20"; //It's a dynamic var generate via date()
UPDATE table_name SET starting_date = DATE_ADD('2022-07-20',(INTERVAL (SELECT GREATEST(activity_1,activity_2,activity_3) FROM table_name WHERE id ='1') MONTH) WHERE id ='1'
My desire is to add 12 months (or the greatest value) to 2022-07-20...
I'm trying several queries with no positive result
Any idea around?
Thanks
Use multiple-table UPDATE syntax.
UPDATE table_name
JOIN ( SELECT id,
GREATEST(activity_1,activity_2,activity_3) interval_for_update
FROM table_name ) value_for_update USING (id)
SET starting_date = '2022-07-20' + INTERVAL interval_for_update MONTH
-- WHERE id = 1
PS. Never enclose numeric literal values with the quote chars - this converts them to strings and causes excess implicit data convertions.
So I have figured out how to generate a report that will count items for each month based upon a WHERE criteria like:
SELECT
Year(FROM_UNIXTIME(`tTime1`)) as YEAR,
Month(FROM_UNIXTIME(`tTime1`)) as MONTH,
Count(*) As BUY_RESULT
FROM zupsbackup
WHERE Result='BUY' AND tPeriod='60'
GROUP BY Year(FROM_UNIXTIME(`tTime1`)), Month(FROM_UNIXTIME(`tTime1`))
This produces table result like
YEAR|MONTH|BUY_RESULT
However my Result column has 2 possible values BUY and SELL.
So I want to add another column to the result so that it looks like
YEAR|MONTH|BUY_RESULT|SELL_RESULT
How can I create another column in MYSQL ???
I have tried UNION but this adds the SELL results to the end of the results table and does not create the extra column.
So how do I create an extra column using a WHERE clause so that RESULT='SELL' to be added next to the existing column where RESULT='BUY' ?????
Thanks !!!!!
Use conditional aggregation:
SELECT Year(FROM_UNIXTIME(`tTime1`)) as YEAR, Month(FROM_UNIXTIME(`tTime1`)) as MONTH,
SUM(Result = 'BUY') As BUY_RESULT, SUM(Result = 'SELL') as SELL_RESULT
FROM zupsbackup
WHERE tPeriod = '60'
GROUP BY Year(FROM_UNIXTIME(`tTime1`)), Month(FROM_UNIXTIME(`tTime1`));
An easy way to accomplish this is to use a sub-query:
SELECT
Year(FROM_UNIXTIME(tTime1)) as YEAR,
Month(FROM_UNIXTIME(tTime1)) as MONTH,
Count(*) As BUY_RESULT,
(
SELECT
COUNT(sub.*)
FROM
zupsbackup AS sub
WHERE
sub.Result = 'SELL' and
sub.tPeriod = '60' AND
Year(FROM_UNIXTIME(sub.tTime1)) = YEAR AND
Month(FROM_UNIXTIME(tTime1)) = MONTH
) AS SELL_RESULT
FROM
zupsbackup
WHERE
Result='BUY' AND
tPeriod='60'
GROUP BY
Year(FROM_UNIXTIME(tTime1)),
Month(FROM_UNIXTIME(tTime1))
I have a table with the following format:
offer_id consumer_id date
1 1 1282454200
1 1 1282453200
2 2 1282453240
1 3 1282455200
2 1 1282453210
"date" is in unix format.
I need to count all of the daily entries, so if I have 10 entries from yesterday and 8 entries from today, I should get:
2013-06-23 10
2013-06-24 8
This is part of my work on trying to optimize code, so far I have been doing this via PHP code, but you can imagine what happens with a growing database :). This is my php (codeigniter) attempt that I'm trying (unsuccessfully) to translate into mysql:
foreach ($offers as $item) {
$date = $item->date;
$day_date = date("Y-m-d", $date);
$day_start = strtotime(date("Y-m-d 00:00:00", $date));
$day_end = strtotime(date("Y-m-d 23:59:59", $date));
if (!in_array($day_date, $day_array)) {
$day_array[] = $day_date;
$this->db->where("date >=", $day_start);
$this->db->where("date <=", $day_end);
$this->db->from("offers_consumers_history");
$total_points = $this->db->count_all_results();
$db_date = array($day_date, $total_points);
$data[] = $db_date;
}
}
I basically grabbed all of the entries in a previous query and went through every date, if the date isn't in my final array, I had to it by counting all results from 00:00:00 to 23:59:59.
Looking for help in building equivalent SQL code.
You could use this SQL query:
SELECT DATE(FROM_UNIXTIME(date)), COUNT(*)
FROM offers_consumers_history
GROUP BY DATE(FROM_UNIXTIME(date))
Please see fiddle here.
Try like
SELECT count(*) as cnt , date FROM `my_table` GROUP BY date
Then you can change them as your required format.It is simple and same that to change the dates into FROM_UNIXTIME and then counting
If I have right understood your question, group by is what you need
I am in a problem and i am confused. I have a table for events and in it there is a column that holds the timestamp of the date time when it was created. I want to get all events(rows) that were created in this month, the last month, and 2 more month before the last month. So, basically 4 months. I want to show them separately so, i can think i can query the datebase for all rows in this month in one query and another query for previous month and so on for 4 months. The problem is i don't know how can i do that. There is a month() function in mysql but i am lost, no idea how to use it.
Any help would be very much appreciated. Thanks
Edit: table structure is like this.
Title | Uri | created
Event | Event| 1337782223
name | uri |
.......
EDIT 2
Thanks all for your help..
I tried it myself and with my friend google...:)
I used this
SELECT * FROM `events` WHERE MONTH(FROM_UNIXTIME(created)) = $month;
and this seems to work for me, i will just pass the month whos rows i want from php thats easier for me...lol
Thanks again to all for your answers its appreciated. This place is awesome
Select * From your_table Group By Month(column_with_date);
Of couse you have to have a timestamp column in your table.
If not then it's impossible to restore the informations when the entry was inserted.
$month = '5,4,3,2';
select colum_name from table_name where month(table_field_name) IN ($thismonth) Group By month(table_field_name)
try this one if you are using separate query for each month
$todayDate = date("Y-m-d");
$thismonth = strtotime($todayDate);
$previous_month = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
and you can get past months just do minus NUMBER ex -2,-3 .....for current month
select colum_name from table_name where month(table_field_name) = month ($thismonth) AND year(table_field_name) = year ($thismonth) Group By month(table_field_name);
for previous month
select colum_name from table_name where month(table_field_name) = month ($previous_month ) AND year(table_field_name) = year ($thismonth) Group By month(table_field_name)
I run an online magazine and would like a dead-simple way to track 3 metrics:
How many comments are left each day.
How many links my users submit.
How many new members I get each day.
This information is all my database, I'm just not sure how to get it out.
I have a "comments" table with about 3500 comments. Each comment gets a row in the table. One of the columns in the table is "timestamp."
I'm assuming there's a query that will select this table, sort the rows by timestamp, group them in 24-hour increments and then count the number of rows in each group - telling me how many new comments I received each day.
What would that query look like? I think I can figure out the other ones if I had the first one working.
This fragment will display your results in a themed table:
$sq = 'SELECT COUNT(*) cnt, DATE(FROM_UNIXTIME(timestamp)) day '
. 'FROM {comments} c '
. 'GROUP BY 2 '
. 'ORDER BY 2 DESC';
$q = db_query($sq);
$stats = array();
while ($o = db_fetch_object($q)) {
$stats[$o->day] = array($o->day, $o->cnt);
}
return theme('table', NULL, $stats));
Using DATE(timestamp) doesn't work because comments.timestamp is in UNIX_TIMESTAMP format, whereas DATE() expects an ASCII date.
Use the date_format() function to format the dates;
select count(id) as commentCount, date_format(dateColumn, '%Y-%m-%d') as commentDate
from yourTable
group by commentDate