Here is the problem. I need to transform this query to get the right information and Im not sure how.
Database info
Id | Client_Code | Time_Date | Employee_Name | Date_Time | Time_Stamp
1 | 000010 | 2010-11-17 07:45:00| Jerry | 2010-11-17| 07:45 AM
2 | 000022 | 2010-11-17 07:30:00| Jerry | 2010-11-17| 07:30 AM
3 | 000010 | 2010-11-17 16:00:00| Bill | 2010-11-17| 04:00 PM
4 | 000022 | 2010-11-17 16:00:00| Bill | 2010-11-17| 04:00 PM
Here is the query
$sql = "SELECT Client_Code, MAX(TIME(Time_Date)), Employee_Name, Date_Time, Time_Stamp FROM Transaction WHERE Date_Time = CURdate()
AND Time_Stamp != '' GROUP BY Client_Code";
Here is what i get with this query and phpcode
echo " $row[Employee_Name], $row[Client_Code], ".$row['MAX(TIME(Time_Date))']."<br>";
Jerry, 000010, 16:00:00
Bill, 000022, 16:00:00
For some reason yes its giving me the right 16:00:00 Time but it is not giving me the right employee name with that time. It has something to do with the grouping I think but the group has to be by client_code first because I want the most recent entry for each Client_Code. Also I can not use ID for grouping because inputs are not always in order. Here is what is should look like.
Bill, 000010, 16:00:00
Bill, 000022, 16:00:00
Can anyone tell me how to fix this query to get the correct information please. Also once the mysql query is corrected Ill need it to count each employee up and display the results like so.
Bill, 2
Use a self join:
SELECT x.client_code,
TIME(x.time_date)
x.employee_name,
x.date_time,
x.time_stamp
FROM TRANSACTION x
JOIN (SELECT t.client_code,
MAX(t.time_date) AS max_time_date
FROM TRANSACTION t
WHERE t.date_time = CURRENT_DATE
AND t.time_stamp != ''
GROUP BY t.client_code) y ON y.client_code = x.client_code
AND y.max_time_date = x.time_date
WHERE x.date_time = CURRENT_DATE
AND x.time_stamp != ''
You have to add "order by id desc limit 0,1" to your query or you can try to mysql_last_insert_id
Related
I have this query that selects every appointment with status Pending. Works well. The problem with this query it will also select appointments that are pending in the past. I only want to display those that are either today at a later hour than current_time or simply at a later date. Time and date are in a different column. In the example below only the second and third row should be returned. I'm giving you the full query as it is used and working in my app right now. How can this be achieved?
user_schedule table
id | customer_id | date | time | cleaning_status
1 | 345 | 2020-06-09 | 08:00:00 | Pending
2 | 768 | 2020-06-09 | 19:00:00 | Pending
3 | 913 | 2020-06-11 | 07:00:00 | Pending
PHP
if(!empty($_POST)){
//variables
$current_time ='16:00:00';
$current_date ='2020-06-09';
$my_city ='Miami';
$sstatus_o = 'Pending';
//query
$data = $conn->prepare("select *,us.id as orderid,us.customer_id
as ownerId from user_schedule us
left join users u
on us.customer_id=u.id
LEFT JOIN user_avatar ua
ON us.customer_id=ua.user_id
and ua.last_update = (select
max(last_update)
from user_avatar ua1 where
ua.user_id=ua1.user_id)
left join user_address uad
on us.customer_id=uad.user_id
where (uad.city LIKE ?) AND
us.cleaning_status=? ORDER BY us.id DESC");
$data->bind_param('ss',$my_city,$sstatus_o);
$data->execute();
$result_data = $data->get_result();
}
You can add another constraint to your query that checks whether the timestamp formed from your date and time values is greater than your $current_date and $current_time values i.e.
WHERE uad.city LIKE ?
AND us.cleaning_status = ?
AND TIMESTAMP(us.date, us.time) > TIMESTAMP(?, ?)
and then add the $current_date and $current_time variables to the bind_param i.e.
$data->bind_param('ssss', $my_city, $sstatus_o, $current_date, $current_time);
Table example
id | name | value | date
--------------------------------------------
1 | abc | 20 | 2018-01-26
1 | abc | 24 | 2018-01-27
1 | abc | 25 | 2018-01-28
1 | abc | 30 | 2018-01-29
I know how to fetch data from 28th Jan or today. But I need some way to show values of two dates in two columns. Is it possible in one mysql query?
Like this
name | value_today | value_pre
---------------------------------------
abc | 30 | 25
You can achieve this using the case. This query returns the today's value and previous date value:
SELECT
id, name,
Sum(Case When date = CURDATE()
Then value Else 0 End) TodaySum,
Sum(Case When (date = CURDATE()-1)
Then value Else 0 End) PreviousSum
FROM tbl1
group by id, name
Here's a hypothetical query that would do it.
SELECT t1.value AS value_today,(SELECT t2.value FROM table AS t2 WHERE t2.date=2018-01-29 ) AS value_pre
FROM table AS t1 WHERE t1.date=2018-01-28
How can I get the latest data of a specific day in MySQL?
Let's assume that I have a column of dates recorded on my database
dates | time | value
---------------------------------------------
2015-08-05 | 11:03:02 | 200
2015-08-05 | 23:04:22 | 2400
2015-08-07 | 8:00:22 | 500
2015-08-08 | 13:00:11 | 400
2015-08-08 | 13:23:11 | 200
2015-08-09 | 17:00:23 | 2200
2015-08-09 | 17:06:00 | 1290
2015-08-09 | 19:22:00 | 900
2015-08-13 | 01:01:22 | 1010
I want to get the latest data or transaction of a specific date, my desired result would be like this
dates | time | value
---------------------------------------------
2015-08-05 | 23:04:22 | 2400
2015-08-07 | 8:00:22 | 500
2015-08-08 | 13:23:11 | 200
2015-08-09 | 19:22:00 | 900
2015-08-13 | 01:01:22 | 1010
Only the latest data of a spefic or distinct date is chosen, what is the possible query with this?
You need to do this way to get that for each dates
select t1.dates,t1.time,t1.value from table as t1 inner join
(
select dates,max(time) as time from table group by dates
) as t2 on t1.dates=t2.dates and t1.time=t2.time
Use
SELECT * FROM TABLE_NAME GROUP BY dates ORDER BY time desc LIMIT 1;
Or
SELECT * FROM TABLE_NAME GROUP BY dates HAVING time <= '23:59:59' LIMIT 1;
Try this:
SELECT `dates`, MAX(`time`) AS `time`, MAX(`value`) AS `value`
FROM `tbl_name`
GROUP BY `dates`
ORDER BY `dates` ASC
Try this query:
SELECT * FROM `table` GROUP BY dates ORDER BY time DESC;
i have two table in mysql phpmyadmin
1) attend
user_name
date
checkin
2) attendout
user_name
date
checkout
here is a table
attend table
_____________________________________________________
user_name | date | checkin
abcd | 10-5-2015 | 2:00
xyz | 10-5-2015 | 3:00
attendout table
_____________________________________________________
user_name | date | checkout
abcd | 10-5-2015 | 5:00
xyz | 10-5-2015 | 6:00
i want result like this
_____________________________________________________
User | Date | Check in | Check out
abcd | 10-5-2015 | 2:00 | 5:00
xyz | 10-5-2015 | 3:00 | 6:00
here is mysql code
$result1 = mysql_query("SELECT attend.user_name,attend.date,attend.checkin, attendout.checkout ".
"FROM attend, attendout ".
"WHERE attend.date BETWEEN '$a' AND '$b' = attendout.date BETWEEN '$a' AND '$b'");
plz help me to fix it
It is a simple join:
SELECT attend.user_name,attend.date,attend.checkin, attendout.checkout ".
"FROM attend join attendout on attend.user_name=attendout.user_name ".
"WHERE attend.date BETWEEN '$a' AND '$b' = attendout.date BETWEEN '$a' AND '$b'
But stop using deprecated mysql_ API use mysqli_* or PDOwith prepared statements.
IF there is always a 1-1 entry in the system i.e. one entry for checkout for checkin for one date then you should try the left join as follows
SELECT at.user, at.date, at.checkin, ao.checkout from attend at LEFT JOIN attendout ao ON at.date = ao.date
The where condition is also optional. You might or might not add it.
you have to use inner join.
SELECT * FROM attend
INNER JOIN attendout
ON attend.user_name=attendout.user_name
ORDER BY attend.user_name
It may work fine
SELECT user_name,date,checkin,NULL
FROM attend
UNION ALL
SELECT user_name,date,NULL,checkout
FROM attendout
MYSQL Table trial_list structure as follows...
id | product_id | expiry_date(date) | by_user | curr_datentime(timestamp)
we are able to extend any trial, and if we do that it simply another row with new expiry_date.
Now we would like to get rows got expired yesterday, we are currently using following sql query.....
Sample MYSQL DATASET
+----+-------------+-------------+-------------+----------+---------------------+
| id | product_id | comment | expiry_date | by_user | dnt |
+----+-------------+-------------+-------------+----------+---------------------+
| 2 | 50 | testing | 2011-02-18 | tester | 2011-02-17 23:36:12 |
+----+-------------+-------------+-------------+----------+---------------------+
| 3 | 50 | again | 2011-02-20 | tester | 2011-02-19 20:36:12 |
+----+-------------+-------------+-------------+----------+---------------------+
| 4 | 50 | extend | 2011-02-23 | tester | 2011-02-21 22:36:12 |
+----+-------------+-------------+-------------+----------+---------------------+
$sql = 'SELECT id, product_id, expiry_date, by_user, curr_datentime FROM trial_list WHERE expiry_date < CURDATE() ORDER BY expiry_date DESC';
We believe this is not correct as its getting all rows which date is older than yesterday not updated expiry_date, suppose we have given some user expiry date 1st feb 2011 and then we change again with 12th feb 2011, so it selects 1st feb 2011 entry. I think it makes sense.
What you have to do first is get the latest item per product_id. After that you can further filter it down to those which are expired. Something like:
SELECT a.* FROM
trial_list AS a
LEFT JOIN trial_list AS b ON a.product_id = b.product_id AND a.id < b.id
WHERE b.product_id IS NULL
AND a.expiry_date < curdate()
See http://dev.mysql.com/doc/refman/5.5/en/example-maximum-row.html
Try using NOW() instead of CURDATE(), you are comparing a Date to a Timestamp, NOW() will compare timestamps.