How many rows contain a specific value using php and mysqli - php

I have a table "bookings" that looks like
bookings
id id_class date day name
1 2 2016-03-10 16:00:00 monday Fredrik
2 5 2016-03-11 16:00:00 tuesday Richard
3 7 2016-03-11 18:00:00 tuesday Sara
4 4 2016-03-11 15:00:00 tuesday Fredrik
Then I extract that table using php
<?php
$result = $con->query("select * from bookings");
<?
What I want now is a php function that tells me how many rows that contain a specific entry such as "id_class=2" or how many rows contain (name=Fredrik) where the answer should be between 0 and a positive number. Is there a way for this?

Use where to get specific condition
SELECT COUNT(*) FROM bookings WHERE id_class=2
SELECT COUNT(*) FROM bookings WHERE name='Fredrik'

Related

Is it possible to loop the result of a query that gets each revenue for the last 8 weeks based on the input date of the user?

User input = '2017-03-12'
Let say I have this tableRevenue
date revenue
---------- ---------
2017-01-01 100
2017-01-08 100
2017-01-15 100
2017-01-22 100
2017-01-29 100
2017-01-05 100
2017-01-12 100
2017-02-19 100
2017-02-26 100
2017-03-05 100
2017-03-12 100
And another tableHolidays which contains
date
----------
2017-01-15
2017-02-19
2017-03-05
I want to display it like this:
date revenue
---------- ---------
2017-01-01 100
2017-01-08 100
2017-01-22 100
2017-01-29 100
2017-01-05 100
2017-01-12 100
2017-02-26 100
2017-03-12 100
I want to display the revenue each of the last 8 weeks and I want to skip all the dates that are existing in tableHolidays using a loop. Is this possible in PHP?
mention: you didn't tag any specific database - my answer will refer to SQL-Server:
assuming #UserDate is a variable with the user input date
Use Date-Functions (specific to every DB system) to calculate the date range. in your case to subtract the 8 weeks.
Select all rows within this date range
exclude (NOT IN) all dates from your resultset which occur in your tableHolidays table
GROUP BY weeks (calculate weeknumber with WEEK) and SUM the revenue
Query:
SELECT WEEK(tR.date) as weeknr
,SUM(tR.revenue)
FROM tableRevenue tR
WHERE tR.date >= DATEADD(wk,-8,#UserDate)
AND tR.date <= #UserDate
AND tR.date NOT IN (SELECT date FROM tableHolidays)
GROUP BY WEEK(tR.date)
you can use the 'ANY' which is a mysql operator
for more information you can visit this link
https://www.w3schools.com/sql/sql_any_all.asp
$userInput = '2017-03-12';
$sql = "SELECT `date`, `revenue`
FROM `tableRevenue`
WHERE (
(`date` = ANY
(SELECT `date` FROM `tableHolidays` WHERE DATE(date)<='{$userInput}'))
AND (DATE(date) <='{$userInput}')
)";

How can i get all months With between two dates

i have this query that get all regsiterd data between this two dates
SELECT date as date_month, status FROM tbl_reports WHERE status != 0 AND (date BETWEEN '2017-01-01' AND '2017-12-31') GROUP BY MONTH(date)
result
date_month
2017-04-19
2017-05-24
2017-06-26
2017-07-01
but how i can get the result even if there is no regsitered input in the other months
want result
Date_month
2017-01-01
2017-02-01
2017-03-01 - it will give this value if there is no such data in this month
2017-04-19 - i will get the input date from database
'' '' ''
2017-12-31
is it possible ? or i need some php code to manipulate this data?
thank you in advance for my answering my question.. :)
Create an extra table, named datehelper. Provide it with 2 columns: year and month. And fill those with year: 2017 until 2027 and 1 / 12 for the months.
SELECT
datehelper.year,
datehelper.month,
r.status
FROM datehelper
LEFT JOIN tbl_reports r ON MONTH(r.date) = datehelper.month
AND YEAR(r.date) = datehelper.year AND status != 0
WHERE (datehelper.year = '2017')
ORDER BY datehelper.year, datehelper.month
I see you would get duplicates on the left joined part if there are more records there in a certain month.
What status would you expect? Let's think there are records with status 0 (excluded) status = 1, and status = 2
datehelper:
year month
2017 1
2017 2
2017 3
2017 4
...
2017 11
2017 12
2018 1
...

Php, MySql select records within current year, month range

I'm trying to create a query to select records that fit current year month range. Here is my database table:
id name date_start date_end
======================================
1 John 2016-05-20 2018-01-01
2 Peter 2017-02-02 2017-05-10
3 Mike 2015-02-02 2017-02-06
and my query:
SELECT * FROM users
WHERE YEAR(date_start) = 2017
AND MONTH(date_start) = 01
OR YEAR(date_end) = 2017
AND MONTH(date_end) = 01
So basically I need to select all users where current year/month in this case 2017/01 is within start/end column range.
In above query, only John and Mike records should be selected.
I think you just want to find out which users have the date 2017-01-01 within their ranges, defined by the date_start and date_end. If so, then the following query should work:
SELECT *
FROM users
WHERE date_start <= '2017-01-01' AND date_end >= '2017-01-01'
Demo here:
SQLFiddle

Getting rows of a day by timestamp, Day/Minute of certain pattern

id name quantity timestamp
1 item1 2 2015-06-01 20:00:00
2 item2 5 2015-06-01 22:30:00
3 item3 2 2015-06-02 20:00:00
4 item4 7 2015-06-02 20:30:00
5 item5 9 2015-06-02 21:30:00
This is an example database, 'timestamp' is in datetime format and contains varying values. Note that the table contains various date and time data. How do i get all the rows of one day and of certain minute(above eg: 30) only, eg how can i select all the rows with timestamp 2015-06-01 **:30:00, here ** = hour any help, thanks.
You can use DATE & MINUTE functions -
SELECT * from your_table WHERE DATE(`timestamp`) = '2015-06-02' AND MINUTE(`timestamp`) = 30
You need to write follow query:
SELECT * FROM Your_Table_Name WHERE DATE(timestamp) = '2015-06-01' AND TIME(timestamp) LIKE '%:30:00'

Update statement with join and two where conditions

This is what I need the update to do:
update my summary table TIME column with the user-edited TIME value
compare the new TIME, using ID's from my summary table and hour_interval table to join tables
find the correct row ID where the new TIME falls within (there are two columns with start and end hours; example would be: start column - 04:00:00 end column 06:00:00)
update the summary table with the correct interval row ID using the summary table ID
The code executes, but doesn't update the interval ID. Suggestions?
UPDATE summary S
JOIN hour_interval H
ON S.hourinterval_id = H.hourinterval_id
SET S.hourinterval_id = H.hourinterval_id
WHERE ('$new_time' BETWEEN H.start_hour AND H.end_hour)
AND summary_id = '$summary_id'"
hourinterval_id start_hour end_hour
1 4:00:00 5:59:59
2 6:00:00 7:59:59
3 8:00:00 9:59:59
4 10:00:00 11:59:59
5 12:00:00 13:59:59
6 14:00:00 15:59:59
7 16:00:00 17:59:59
8 18:00:00 19:59:59
Try this:
UPDATE summary S
JOIN hour_interval H
ON '$new_time' BETWEEN H.start_hour AND H.end_hour
SET S.hourinterval_id = H.hourinterval_id
WHERE summary_id = '$summary_id'"

Categories