I will run a query and get a list of aID and based on that I will try to match below tables.For each aID I just want one record either from tblGAlert or tblEAlert based on gDateTime
and eDateTime
depends on which time comes first.
CREATE TABLE IF NOT EXISTS `tblGAlert` (
`gAlertID` int(11) NOT NULL AUTO_INCREMENT,
`eID` int(5) NOT NULL,
`aID` int(5) NOT NULL,
`gEntryStatus` enum('Do','Ad','Ej') NOT NULL,
`gDateTime` datetime NOT NULL,
`gInsertDateTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`gMessage` varchar(255) NOT NULL,
PRIMARY KEY (`gAlertID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `tblEAlert` (
`eAlertID` int(11) NOT NULL AUTO_INCREMENT,
`eID` int(5) NOT NULL,
`aID` int(5) NOT NULL,
`eDateTime` datetime NOT NULL,
`eInsertDateTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`eMessage` varchar(255) NOT NULL,
PRIMARY KEY (`eAlertID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Sample data I say for tblGAlert
1,12,1122,'Do',2013-07-13 14:30:19,2013-07-13 15:30:19,'' 2,13,1122,'Ad',2013-07-13 14:35:19,2013-07-13 15:35:19,'' 3,13,1122,'Ad',2013-07-13 14:38:19,2013-07-13 15:39:19,'' 4,14,1122,'Ej',2013-07-13 14:45:19,2013-07-13 15:55:19,'' 5,14,1122,'Ej',2013-07-13 14:50:19,2013-07-13 15:56:19,''
Same data for tblEAlert.
1,1,1122,2013-07-13 14:33:19,2013-07-13 15:35:19,'' 2,5,1122,2013-07-13 14:36:19,2013-07-13 15:36:19,'' 3,6,1122,2013-07-13 14:37:19,2013-07-13 15:39:19,'' 4,7,1122,2013-07-13 14:48:19,2013-07-13 15:55:19,'' 5,8,1122,2013-07-13 14:52:19,2013-07-13 15:56:19,''
The output I want to merge them together in such a way arrange by the gDateTime and eDateTime. Any idea how to merge them ?
I have tried below script but the answer I get is not right and it hogs my db server.
SELECT X . *
FROM (
SELECT gAlertID, 'gType' AS PTYPE, gDateTime AS DATE
FROM tblGAlert
WHERE tblGAlert.aID =2494
UNION
SELECT eAlertID, 'eType' AS PTYPE, eDateTime AS DATE
FROM tblEAlert
WHERE tblEAlert.aID =2494
)X
ORDER BY X.`date`
Best Answer
First of all, this is my first post on dba.stackexchange.com, so I apologize in advance if there is any etiquette that I fail to follow, and am open to your input as to how I can improve future posts.
Best database design practices would call for consolidating the two very similar tables into just one table, like sa555 described. The issue you are currently facing is the direct consequence of having these two tables separate.
However, since you are working with a live system, I would recommend asking yourself and/or the rest of your development team (if applicable) the following question:
If the benefits outweigh the costs, I would recommend consolidating the two tables, just as sa555 recommended. If not, I have included the MySQL code below to create a stored procedure that will return the most recent row from either table. Just remember that this is a mere surface-level fix, and that your database still contains a design flaw that could impede future development.
Here are some important notes on the procedure's edge-case behavior:
SQL Query (creates stored procedure):
Now just execute
CALL GetLatestAlert();
and the most recent row from either table will be returned.