简体   繁体   中英

left join not return the all the rows from left table in mysql

I really didn't find out that why left join does not return all the rows form t1. Please any help would appreciated.

t1 schema:

 CREATE TABLE `tbl_assigned` (
     `ID` int(11) NOT NULL AUTO_INCREMENT,
     `Round` int(2) NOT NULL,
     `TraineeID` varchar(7) NOT NULL,
     `Name` varchar(25) NOT NULL,
     `Mobile` int(10) DEFAULT NULL,
     `BatchID` varchar(35) NOT NULL,
     `Remarks` varchar(8) DEFAULT NULL,
     `District` varchar(15) DEFAULT NULL,
     `Comments` varchar(21) DEFAULT NULL,
     `Level` varchar(2) DEFAULT NULL,
     `Trade` int(1) DEFAULT NULL,
     `Status` int(1) DEFAULT NULL,
     `Photo` varchar(50) DEFAULT NULL,
     PRIMARY KEY (`ID`),
     KEY `TraineeID` (`TraineeID`)
    ) ENGINE=InnoDB AUTO_INCREMENT=356 DEFAULT CHARSET=utf8

t2 schema:

CREATE TABLE `tbl_attn_temp` (
 `ID` int(11) NOT NULL AUTO_INCREMENT,
 `TraineeID` varchar(7) NOT NULL,
 `ScannerID` int(7) NOT NULL,
 `attnDate` date NOT NULL,
 `attnTime` time NOT NULL,
 `Status` tinyint(1) NOT NULL DEFAULT '1',
 PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=2135 DEFAULT CHARSET=latin1

query:

SELECT t1.TraineeID, t2.attnDate
FROM tbl_assigned t1
LEFT JOIN tbl_attn_temp t2 on t1.TraineeID=t2.TraineeID
WHERE t1.BatchID='ID-Welding/SSTS-01M/R7/01' and t2.attnDate='2015-12-28'

Output:

TID   attnDate
15950 2015-12-28
24310 2015-12-28
24317 2015-12-28
24327 2015-12-28
24400 2015-12-28
24973 2015-12-28
25186 2015-12-28
25281 2015-12-28
25285 2015-12-28
25300 2015-12-28

but t1 table has 15 TraineeID with this criteria.

SELECT t1.TraineeID FROM tbl_assigned t1 WHERE t1.BatchID='ID-Welding/SSTS-01M/R7/01'

output:

15950
20012
21173
24310
24317
24327
24400
24936
24973
25033
25186
25281
25282
25285
25300

SELECT t2.TraineeID, t2.attnDate FROM tbl_attn_temp t2 WHERE t2.attnDate='2015-12-28'

Output:

ID        Date
SSTS001 2015-12-28
SSTS001 2015-12-28
SSTS001 2015-12-28
15950 2015-12-28
24317 2015-12-28
24738 2015-12-28
25186 2015-12-28
25281 2015-12-28
24973 2015-12-28
24310 2015-12-28
24400 2015-12-28
24327 2015-12-28
25300 2015-12-28
25285 2015-12-28
SSTS002 2015-12-28
28702 2015-12-28
28702 2015-12-28
22934 2015-12-28
26620 2015-12-28
24068 2015-12-28
21343 2015-12-28
1151467 2015-12-28
24931 2015-12-28
24931 2015-12-28
24931 2015-12-28
4872 2015-12-28
24071 2015-12-28
24786 2015-12-28
6203 2015-12-28
24069 2015-12-28

Check if there is issue of space in the BatchID field. Space may be in the beginning and end of missing records.

Also check date of other records as well.

To tackle with space issue which may be in the beginning and end of records

use this query.

SELECT t1.TraineeID, t2.attnDate
FROM tbl_assigned t1
LEFT JOIN tbl_attn_temp t2 on t1.TraineeID=t2.TraineeID
WHERE ifnull(trim(t1.BatchID),'')='ID-Welding/SSTS-01M/R7/01'

if you get expected records then check if date is '2015-12-28' in other records as well.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM