Hi,
These are two tables of my MySQL database 8.0.17
On these tables the corresponding column for each tables is the column tUnity
-- ------------------------------ Table structure for t_release-- ----------------------------DROPTABLEIFEXISTS`t_release`;CREATETABLE`t_release` (`tID`int(11) NOTNULL AUTO_INCREMENT,`tUnity`varchar(255) DEFAULTNULL,`tMonthYear`varchar(255) DEFAULTNULL,`tHHrelease` datetime(0) DEFAULTNULL,`tHHapproved` datetime(0) DEFAULTNULL,
PRIMARY KEY (`tID`) USING BTREE
) ENGINE = InnoDB;-- ------------------------------ Records of t_release-- ----------------------------INSERTINTO`t_release`VALUES (1, 'D41', '1-2021', '2021-02-26 16:22:19', NULL);-- ------------------------------ Table structure for t_unities-- ----------------------------DROPTABLEIFEXISTS`t_unities`;CREATETABLE`t_unities` (`tID`int(11) NOTNULL AUTO_INCREMENT,`tUnity`varchar(255) DEFAULTNULL,
PRIMARY KEY (`tID`) USING BTREE
) ENGINE = InnoDB;-- ------------------------------ Records of t_unities-- ----------------------------INSERTINTO`t_unities`VALUES (1, 'D40');INSERTINTO`t_unities`VALUES (2, 'D41');INSERTINTO`t_unities`VALUES (3, 'D42');INSERTINTO`t_unities`VALUES (4, 'D43');INSERTINTO`t_unities`VALUES (5, 'D44');INSERTINTO`t_unities`VALUES (6, 'D45');INSERTINTO`t_unities`VALUES (7, 'D46');INSERTINTO`t_unities`VALUES (8, 'D47');INSERTINTO`t_unities`VALUES (9, 'D48');INSERTINTO`t_unities`VALUES (10, 'D49');
I need union these tables for this return
+--------+------------+---------------------+-------------+--------+| qUnity | tMonthYear | tHHrelease | tHHapproved | tUnity |+--------+------------+---------------------+-------------+--------+| D40 || | NULL | D40 || D41 |1-2021| 2021-02-26 16:22:19 | NULL | D41 || D42 || | NULL | D42 || D43 || | NULL | D43 || D44 || | NULL | D43 || D45 || | NULL | D45 || D46 || | NULL | D46 || D47 || | NULL | D47 || D48 || | NULL | D48 || D49 || | NULL | D49 |+--------+------------+---------------------+-------------+--------+10 rows in set (0.03 sec)
And I have tried this SQL query
SELECTDISTINCT
q.tUnity AS qUnity,
t.tMonthYear,
t.tHHrelease,
t.tHHapproved,
t.tUnity AS tUnityFROM
t_release tLEFTJOIN t_unities q ONLEFT ( t.tUnity, 2 ) = LEFT ( q.tUnity, 2 ) WHERELEFT ( t.tUnity, 2 ) = LEFT ( 'd400', 2 ) AND tMonthYear = '1-2021';
But the return from this SQL query is
+--------+------------+---------------------+-------------+--------+| qUnity | tMonthYear | tHHrelease | tHHapproved | tUnity |+--------+------------+---------------------+-------------+--------+| D40 |1-2021| 2021-02-26 16:22:19 | NULL | D41 || D41 |1-2021| 2021-02-26 16:22:19 | NULL | D41 || D42 |1-2021| 2021-02-26 16:22:19 | NULL | D41 || D43 |1-2021| 2021-02-26 16:22:19 | NULL | D41 || D44 |1-2021| 2021-02-26 16:22:19 | NULL | D41 || D45 |1-2021| 2021-02-26 16:22:19 | NULL | D41 || D46 |1-2021| 2021-02-26 16:22:19 | NULL | D41 || D47 |1-2021| 2021-02-26 16:22:19 | NULL | D41 || D48 |1-2021| 2021-02-26 16:22:19 | NULL | D41 || D49 |1-2021| 2021-02-26 16:22:19 | NULL | D41 |+--------+------------+---------------------+-------------+--------+10 rows in set (0.03 sec)
Help me to do it.