how to show 1st row if 2nd table row is not found please see the example thanks
$result1 = mysql_query("
SELECT a.user_name
, a.date
, a.checkin
, o.checkout
FROM attend a
LEFT
JOIN attendout o
ON a.user_name = o.user_name
AND a.date = o.date
WHERE a.date BETWEEN '2015-06-01' AND '2015-06-09'
AND o.user_name = 'salman';
");
now it is showing like this
username date checkin checkout
salman 2015-06-01 11:31:34 17:23:47
salman 2015-06-02 11:19:23 17:15:15
salman 2015-06-03 11:48:22 18:16:27
salman 2015-06-06 11:39:26 16:56:13
salman 2015-06-07 11:24:59 17:36:01
and i want like this
username date checkin checkout
salman 2015-06-01 11:31:34 17:23:47
salman 2015-06-02 11:19:23 17:15:15
salman 2015-06-03 11:48:22 18:16:27
salman 2015-06-06 11:39:26 16:56:13
salman 2015-06-07 11:24:59 17:36:01
salman 2015-06-08 12:24:59
salman 2015-06-09 10:24:59
because some time checkout row is null
Move the condition on the second table from the WHERE
to the ON
:
SELECT a.user_name, a.date, a.checkin, o.checkout
FROM attend a LEFT JOIN
attendout o
ON a.user_name = o.user_name AND
a.date = o.date AND o.user_name = 'salman'
WHERE a.date BETWEEN '2015-06-01' AND '2015-06-09' ;
When you put the condition in the WHERE
clause you are turning the LEFT JOIN
into an INNER JOIN
, because the NULL
values are filtered out.
Add condition for checkout column:
SELECT attend.user_name,attend.date,attend.checkin, attendout.checkout
FROM attend
left join attendout on attend.user_name=attendout.user_name AND attend.date=attendout.date
WHERE attend.date BETWEEN '2015-06-01'
AND '2015-06-09'
AND attendout.user_name='salman'
AND attendout.checkout IS NOT NULL
or use != operator if value can be empty
Full code: if you dont have any records, replacing LEFT JOIN to INNER JOIN will work for you
$result1 = mysql_query("
SELECT a.user_name
, a.date
, a.checkin
, o.checkout
FROM attend a
JOIN attendout o
ON a.user_name = o.user_name
AND a.date = o.date
WHERE a.date BETWEEN '2015-06-01' AND '2015-06-09'
AND o.user_name = 'salman';
");
Otherwise use:
$result1 = mysql_query("
SELECT a.user_name
, a.date
, a.checkin
, o.checkout
FROM attend a
LEFT JOIN attendout o
ON a.user_name = o.user_name
AND a.date = o.date
WHERE a.date BETWEEN '2015-06-01' AND '2015-06-09'
AND o.user_name = 'salman'
AND o.checkout != ''
AND o.checkout IS NOT NULL;
");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.