My Model function is as below:
function get_newJoineesList($limit=''){
$SQL ="
SELECT userid as uid, CONCAT( firstname, ' ', lastname ) AS name,profileimage, joining_date, role as designation
FROM pr_users_details
INNER JOIN pr_users ON pr_users.id = pr_users_details.userid
LEFT JOIN pr_userroles ON pr_userroles.id=pr_users.userroleid
WHERE joining_date > DATE_SUB( NOW()-1 , INTERVAL 15 DAY )
ORDER BY pr_users_details.id DESC";
if($limit!=''){
$SQL.=" LIMIT ".$limit;
}
$query = $this->db->query($SQL);
$return = $query->result_array();
return $return;
}
I want to get all the results from 15 days ago to now i.e if today is 15th then retrieve all the joining date that falls between 1-15th. But somehow the above query does not work. I am also restricted to use only varchar(15) type as changing this will affect other modules.
snapshot from db2:
snapshot from db1:
The problem is that the way you store the dates do not correspond to how mysql recognizes date literals, therefore the date calculations will be incorrect. You must use the str_to_date() function to convert the strings to valid dates:
SELECT userid as uid, CONCAT( firstname, ' ', lastname ) AS name,profileimage, joining_date, role as designation
FROM pr_users_details
INNER JOIN pr_users ON pr_users.id = pr_users_details.userid
LEFT JOIN pr_userroles ON pr_userroles.id=pr_users.userroleid
WHERE str_to_date(joining_date,'%d-%c-%Y') > DATE_SUB( NOW()-1 , INTERVAL 15 DAY )
ORDER BY pr_users_details.id DESC
Also, I would consider using curdate() instead of now() because you deal with dates only, not times. Using now() may have interesting side effects of excluding dates that are exactly 15 days ago because of the time component.
Related
I would like to add search functionality in my view. The problem is that I get data from raw sql like this so I dont know how to filter the results:
$conn = ConnectionManager::get('default');
$stmt = $conn->execute('SELECT c.id as customerid, CONCAT(c.name, " ", c.surname) as customer, s.title as title, s.length, cs.created , DATE_ADD(cs.created, INTERVAL s.length DAY) as expiring_date, DATEDIFF(DATE_ADD(cs.created, INTERVAL s.length DAY), NOW()) as days_left
FROM customers c
JOIN customer_service_types cs on c.id = cs.customer_id
JOIN service_types s on cs.service_type_id = s.id
WHERE s.is_subscription = 1 and DATE_ADD(cs.created, INTERVAL s.length DAY) > DATE_ADD(NOW(), INTERVAL -30 DAY)
ORDER BY expiring_date');
$expiringServices = $stmt->fetchAll('assoc');
//debug($expiringServices,true);
$this->set(compact('expiringServices'));
Is it possible to filter the results by a form input? I want the user to filter the results by entering the name in a text field and compare it with customer field which returns from the sql query. One guess is the fetchAll(PDO::FETCH_FUNC, fun_name) but I dont know how to implement it.
if the user in the 5 hours has more calls in the past 10 hours, then he should tell me as an example "true". If not, he should say false to me.
if($aufruf = $pdo->prepare("
SELECT
profil_aufrufe.id,
profil_aufrufe.user_id,
profil_aufrufe.aufrufer_id,
profil_aufrufe.date
FROM
profil_aufrufe
WHERE
profil_aufrufe.user_id = :user_id AND profil_aufrufe.date ..."
))
I thought for a long time, but found no way how I can write the SQL code.
Here is your code
if($aufruf = $pdo->prepare("
SELECT
profil_aufrufe.id,
profil_aufrufe.user_id,
profil_aufrufe.aufrufer_id,
profil_aufrufe.date
FROM
profil_aufrufe
WHERE
profil_aufrufe.user_id = :user_id AND profil_aufrufe.date ..."
))
first of all you can't take a column date in your mysql table its reserved keyword of mysql
date
and why are you using alias whenever you don't join any table, there is no need of alias i simplify it i change column date to cdate
if($aufruf = $pdo->prepare("
SELECT
id,
user_id,
aufrufer_id,
cdate
FROM
profil_aufrufe
WHERE
user_id = :user_id AND
cdate between DATE_SUB(NOW(),INTERVAL 5 HOUR) and DATE_SUB(NOW(), INTERVAL 10 HOUR) "));
I think this will work for you
Am trying to find the min value from past 30 days, in my table there is one entry for every day, am using this query
SELECT MIN(low), date, low
FROM historical_data
WHERE name = 'bitcoin'
ORDER BY STR_TO_DATE(date,'%d-%m-%Y') DESC
LIMIT 7
But this value not returing the correct value. The structure of my table is
Table structure
And table data which is store is like this
Table data style
Now what i need is to get the minimum low value. But my query not working it give me wrong value which even did not exist in table as well.
Updates:
Here is my updated Table Structure.
enter image description here
And here is my data in this table which look like this
enter image description here
Now if you look at the data, i want to check the name of token omisego and fatch the low value from past 7 days which will be from 2017-12-25 to 2017-12-19
and in this cast the low value is 9.67, but my current query and the query suggested by my some member did not brings the right answer.
Update 2:
http://rextester.com/TDBSV28042
Here it is, basically i have more then 1400 coins and token historical data, which means that there will me more then 1400 entries for same date like 2017-12-25 but having different name, total i have more then 650000 records. so every date have many entries with different names.
To get the lowest row per group you could use following
SELECT a.*
FROM historical_data a
LEFT JOIN historical_data b ON a.name = b.name
AND a.low > b.low
WHERE b.name IS NULL
AND DATE(a.date) >= '2017-12-19' AND DATE(a.date) <= '2017-12-25'
AND a.name = 'omisego'
or
SELECT a.*
FROM historical_data a
JOIN (
SELECT name,MIN(low) low
FROM historical_data
GROUP BY name
) b USING(name,low)
WHERE DATE(a.date) >= '2017-12-19' AND DATE(a.date) <= '2017-12-25'
AND a.name = 'omisego'
DEMO
For last 30 day of 7 days or n days you could write above query as
SELECT a.*, DATE(a.`date`)
FROM historical_data2 a
LEFT JOIN historical_data2 b ON a.name = b.name
AND DATE(b.`date`) >= CURRENT_DATE() - INTERVAL 30 DAY
AND DATE(b.`date`) <= CURRENT_DATE()
AND a.low > b.low
WHERE b.name IS NULL
AND a.name = 'omisego'
AND DATE(a.`date`) >= CURRENT_DATE() - INTERVAL 30 DAY
AND DATE(a.`date`) <= CURRENT_DATE()
;
DEMO
But note it may return more than one records where low value is same, to choose 1 row among these you have specify another criteria to on different attribute
Consider grouping the same and running the clauses
SELECT name, date, MIN(low)
FROM historical_data
GROUP BY name
HAVING name = 'bitcoin'
AND STR_TO_DATE(date, '%M %d,%Y') > DATE_SUB(NOW(), INTERVAL 30 DAY);
Given the structure, the above query should get you your results.
// Try this code ..
SELECT MIN(`date`) AS date1,low
FROM historical_data
WHERE `date` BETWEEN now() - interval 1 month
AND now() ORDER by low ASC;
I just have a quick question, I am doing a cab booking system. I am finding troubles selecting the reservations within the next 2 hours in SQL. I don't need the whole reservation to appear when i select all, I only want within two hours from the time I run the query.
$query= "select Booking_No,
Email_Address,
Customer_Name,
Passenger_Name,
Phone_No,
concat(Unit_Number,'/', Street_Number,' ', Street_Name,',', Suburb) as Unit_Number,
Destination_Suburb,
Pickup_Date,
Pickup_Time from Booking1
where status = 'false'
and
(Pickup_Time >= DATE_FORMAT(NOW(), '%h:00:00') and
Pickup_Time <= DATE_FORMAT(NOW(), '%h') + interval 2 hour)";
Use the 'BETWEEN' clause in your WHERE clause.
WHERE status = 'false' AND Pickup_Time BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 2 HOUR)
NOW() returns a date/time object, DATE_ADD() returns a date/time object. As long as your Pickup_Time is a date/time object, this will work like a treat!
Using PHP/MySQL
I'm trying to create a select statement that gets the data from the least day of the current week (I'm using it to show data on a certain player 'this week'). The week starts on Sunday. Sundays's data may not always exist therefore if the Sunday data isn't found then it would use the next earliest day found, Monday, Tuesday, etc.
My date column is named 'theDate' and the datatype is 'DATE'
The query would need to be something like:
SELECT *
FROM table_name
WHERE name = '$username'
AND [...theDate = earliest day of data found for the current week week]
LIMIT 1
It would return a single row of data.
This is a query I tried for getting the 'this week' data, It doesn't seem to work correctly on Sunday's it shows nothing:
SELECT *
FROM table_name
WHERE playerName = '$username'
AND YEARWEEK(theDate) = YEARWEEK(CURRENT_DATE)
ORDER BY theDate;
This is the query that I'm using to get 'this months' data and it works even if the first day of the months data is not found, it will use the earliest date of data found in the current month/year (this query works perfect for me):
SELECT *
FROM table_name
WHERE playerName = '$username'
AND theDate >= CAST( DATE_FORMAT( NOW(),'%Y-%m-01') AS DATE)
ORDER BY theDate
LIMIT 1
Without trying this, you probably need an inner query:
select *
from table_name tn
where tn.the_date =
(select min(the_date)
from table_name
where WEEKOFYEAR(the_date) = WEEKOFYEAR(CURDATE())
and YEAR(the_date) = YEAR(CURDATE()))
viz, give me the row(s) in the table with a date equal to the earliest date in the table in the current week and year.
Try this
SELECT * FROM table_name WHERE name = '$username'
AND your_data IS NOT NULL
AND WEEK(the_date,0 = WEEK(NOW(),0))
ORDER BY DATE_FORMAT(the_date,'%w') ASC
Try the following, replace YOUR_DATE with the date from the column you want (theDate):
SELECT ADDDATE(YOUR_DATE, INTERVAL 1-DAYOFWEEK(YOUR_DATE) DAY)
FirstDay from dual
Did you try:
SELECT ADDDATE(theDate , INTERVAL 1-DAYOFWEEK(theDate ) DAY) FirstDay
FROM table_name
WHERE playerName = '$username'
ORDER BY theDate DESC
LIMIT 1