Quantcast
Channel: Oracle, MySQL, Sybase, Informix and other databases
Viewing all articles
Browse latest Browse all 1350

Update join table using MySQL 8.0.17

$
0
0

hi all,

On MySQL database I have two tables

tmp1

mysql>SELECT*FROM`tmp1`;+-----+---------------------+-----------+----------------+--------+| sID | sDate_sHour         | sName     | sStatus        | sValue |+-----+---------------------+-----------+----------------+--------+|1|2019-04-2714:00:52| user76681 |not registered |NULL||2|2019-05-0809:39:19| user76681 |not registered |NULL|+-----+---------------------+-----------+----------------+--------+2rowsinset

And tmp2

mysql>SELECT*FROM`tmp2`;+-----+---------------------+-----------+------------+| sID | sDate_sHour         | sName     | sStatus    |+-----+---------------------+-----------+------------+|1|2019-05-0809:36:14| user76681 | registered |+-----+---------------------+-----------+------------+1rowinset

When I've on tmp2 for the same sName the sDate_sHour higher than the sDate_sHour of tmp1 I need update the column sValue with N value

return tmp1 after update

mysql>SELECT*FROM`tmp1`;+-----+---------------------+-----------+----------------+--------+| sID | sDate_sHour         | sName     | sStatus        | sValue |+-----+---------------------+-----------+----------------+--------+|1|2019-04-2714:00:52| user76681 |not registered | N      ||2|2019-05-0809:39:19| user76681 |not registered |NULL|+-----+---------------------+-----------+----------------+--------+2rowsinset

because the date 2019-05-08 09:36:14 from tmp2 is higher of 2019-04-27 14:00:52 from tmp1 

and the date 2019-05-08 09:36:14 from tmp2 is less of 2019-05-08 09:39:19 from tmp1

I've tried using MySQL 8.0.17 this query without success

mysql> UPDATE `tmp1` ttt
JOIN (
	SELECT
		jjj.sName AS jjjsName
	FROM
		`tmp1` jjj
	JOIN `tmp2` kkk ON jjj.sName = kkk.sName
	AND kkk.sDate_sHour > jjj.sDate_sHour
	WHERE
		jjj.sStatus IN ('not registered')
	ORDER BY
		kkk.sDate_sHour DESC
) AS hhh
SET ttt.sValue = 'N'
WHERE
	ttt.sName = hhh.jjjsName;
Query OK, 2 rows affected

this return is wrong

mysql> SELECT
	*
FROM
	tmp1;+-----+---------------------+-----------+----------------+--------+
| sID | sDate_sHour         | sName     | sStatus        | sValue |+-----+---------------------+-----------+----------------+--------+
|   1 | 2019-04-27 14:00:52 | user76681 | not registered | N      |
|   2 | 2019-05-08 09:39:19 | user76681 | not registered | N      |+-----+---------------------+-----------+----------------+--------+
2 rows in set

Please can you help me?

Structure tables tmp1 and tmp2 below

DROPTABLEIFEXISTS`tmp1`;CREATETABLE`tmp1`(`sID` int(11)NOTNULL AUTO_INCREMENT,`sDate_sHour` datetime DEFAULTNULL,`sName` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULTNULL,`sStatus` varchar(255)DEFAULTNULL,`sValue` char(1)DEFAULTNULL,PRIMARYKEY(`sID`)) ENGINE=InnoDB AUTO_INCREMENT=3DEFAULT CHARSET=utf8;-- ------------------------------ Records of tmp1-- ----------------------------INSERTINTO`tmp1`VALUES('1','2019-04-27 14:00:52','user76681','not registered',null);INSERTINTO`tmp1`VALUES('2','2019-05-08 09:39:19','user76681','not registered',null);DROPTABLEIFEXISTS`tmp2`;CREATETABLE`tmp2`(`sID` int(11)NOTNULL AUTO_INCREMENT,`sDate_sHour` datetime DEFAULTNULL,`sName` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULTNULL,`sStatus` varchar(255)DEFAULTNULL,PRIMARYKEY(`sID`)) ENGINE=InnoDB AUTO_INCREMENT=4DEFAULT CHARSET=utf8;-- ------------------------------ Records of tmp2-- ----------------------------INSERTINTO`tmp2`VALUES('1','2019-05-08 09:36:14','user76681','registered');

Viewing all articles
Browse latest Browse all 1350

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>