how to show two table rows together using mysql - php

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

Related

Selecting entries by date and by time seperately with MySQL

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);

How to fetch multiple duplicate value count in sql

i have database in which user device id and device registerd date (cr_date) stored i want to fetch list of total count of registerd devices on same day with date wise
Here below display table structure
device_ID | cr_date
--------------------
1 | 2016-06-02 18:02:13
2 | 2016-06-02 18:03:58
3 | 2016-06-02 18:04:11
4 | 2016-06-03 18:04:33
5 | 2016-06-03 18:04:33
6 | 2016-06-04 18:04:44
and i want below result
count | date
---------------------------------
3 | 2016-06-02
2 | 2016-06-03
1 | 2016-06-04
i used below query but dosent get success
SELECT device_type, count(*) AS duplicate_count
FROM (
SELECT device_type FROM tbl_app_deviceregister
GROUP BY device_type HAVING COUNT(device_type) > 0
) AS t
Did you want this;)
SELECT count(1) as `count`, date_format(cr_date, '%Y-%m-%d') as `date`
FROM tbl_app_deviceregister
GROUP BY `date`
ORDER BY `count` DESC
You should GROUP BY date value, not by device:
SELECT CAST(cr_date AS DATE) AS `date`, COUNT(device_id)
FROM tbl_app_deviceregister
GROUP BY CAST(cr_date AS DATE)
Your query doesn't seem to reference the same table structure that you've described, but from what I understand, would this work?
SELECT count(id), date_registered FROM (
SELECT
id,
DATE(FROM_UNIXTIME(cr_date)) as date_registered
FROM
tbl_app_deviceregister
) A GROUP BY date_registered
you group it by device_type which is wrong. try to group by date.
select COUNT(*) as count,CONVERT(date,cr_date) as date
from tbl_app_deviceregister
group by CONVERT(date,cr_date)

retrieve the most recent date within a query

I was able to fix my previous query, however the results retrieve contains duplicates. I want to retrieve the latest record.
For example:
id | firstname | lastname | email | date
1 | steven | smith | steven#gmail.com 2013-06-10 04:01:25
2 | Bill | Johnson | bill#gmail.com | 2014-06-10 04:01:25
3 | steven | smith | steven#gmail.com | 2014-10-10 12:01:25
The return result should be the row with IDs 2 and 3
THe ID 1 should be not returned as the date is older than the ID 3 date
How can I add this to the query below ?
SELECT
users.firstname,
users.lastname,
DISTINCT(users.email),
users.pref
FROM (
SELECT
users.firstname,
users.lastname,
users.email,
users.status,
users.active,
CONCAT(
users.preference_1, ',',
users.preference_2, ',',
users.preference_3
) AS pref
FROM users
) AS users
WHERE users.status = 1
AND users.active = 1
AND users.date = (
SELECT MAX(u.date) FROM users AS u WHERE u.email = users.email
)
LIMIT 10000
no need for subselect
add: ORDER BY users.date DESC LIMIT 1 this way you sort the list by date and return only the "1st elemet" which is the one with the "biggest/latest" date
Well, looks like you want to fetch the latest row for each user.
Here's a query that can do it. (It also uses subquery. I don't understand the necessity for using subquery to concat though)
SELECT DISTINCT us.email, us.preference FROM users AS us WHERE us.date = (
SELECT MAX(u.date) FROM users AS u WHERE u.email = us.email
)
This does not fetch all the details you want. But gives you the basic idea about the query you might have to run.

Mysql query group by taking wrong time stamp

I have a query that when executed gives me the result like this
speciaid | title | date
one | xxx | 2013-10-03 02:13:54
one | xxx | 2013-10-03 03:13:54
two | yyy | 2013-10-02 02:13:54
Now if I add GROUP BY specialid, it gets grouped but the date i get is the old date and not the new date for the 'date' column like this
speciaid | title | date
one | xxx | 2013-10-03 02:13:54 // old date selected for entry one
two | yyy | 2013-10-02 02:13:54
and I want it like this
speciaid | title | date
one | xxx | 2013-10-03 03:13:54 // new date for entry one needed
two | yyy | 2013-10-02 02:13:54
I have tried many group by's - I get the same result. I would appreciate it very much if someone help with the group by statement for query.
You're looking for a greatest-n-per-group solution.
Usually solved this way (assuming your result is the table, due to the lack of information in the question).
Option 1:
SELECT * FROM t JOIN (
SELECT title, max(`date`) `date` FROM t
GROUP BY title
) s ON t.title = s.title AND t.`date` = s.`date`
Option 2:
SELECT t1.* FROM t t1
LEFT JOIN t t2
ON t1.title = t2.title AND t1.`date` < t2.`date`
WHERE t2.`date` IS NULL
Working fiddle: http://sqlfiddle.com/#!2/76f2d3/1
If you want to get first/last date from grouped column you need to use max() or min() grouped function.
SELECT speciaid, title, MAX(`date`) FROM table GROUP BY title;
select speciaid, title , date
from
(select * from table_name order by date desc) d
group by title

MYSQL Query most recent entry

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

Categories