sql query for join and subquery

时间:2018-12-03 13:03:41

标签: mysql join subquery

I have 2 table as below

tbl_main

id | number
5  | 98236
6  | 85528
7  | 98236
8  | 98669

tbl_info

id  main_id did epochtime   firstname   lastname    operationstatus
1     6     204 1538384794  rajata      patile      1
2     5     204 1535185544  john        paulo       0
3     7     204 1536667819  jenny       patrick     0
4     6     204 1538384821  koma        mahaj       1   

tbl_info column main_id is foreign key with tbl_main id column.

I want to join both tables group by first tables main_id. If operationstatus value is 0 fetch normal epoch time value. If operationstatus is 1 fetch records with max epochtime.

I tried below query:

SELECT *
FROM `tbl_main` `m`
JOIN `tbl_info` `i` ON `i`.`main_id` = `m`.`id`
WHERE ((`i`.`operationstatus` = '0'AND `i`.`epochtime` > '0')OR (`i`.`operationstatus` = '1' AND `i`.epochtime = (SELECT MAX(epochtime) FROM tbl_info)))
AND `did` = '204'
GROUP BY `m`.`id`
ORDER BY `i`.`epochtime` DESC

It gives 2 records , i need 3 unique records i.e main id 5,6,7. There are 2 records with main id 6 which has operationstatus 1. From this 2 records i need one record which has max epochtime.

id  main_id did epochtime  firstname    lastname    operationstatus
3     7     204 1536667819 jenny        patrick       0
2     5     204 1535185544 john         paulo         0

1 个答案:

答案 0 :(得分:0)

您的查询确实产生了预期的3行

DROP TAble if exists tbl_main,tbl_info;
create table tbl_main(id int, number int);
insert into tbl_main values
(5  , 98236),
(6  , 85528),
(7  , 98236),
(8  , 98669);

create table tbl_info(
id int, main_id int,did int,epochtime varchar(10),  firstname varchar(20),  lastname varchar(20),  operationstatus int);
insert into tbl_info values
(1  ,   6 ,    204, 1538384794 , 'rajata'   ,   'patile'   ,   1),
(2  ,   5 ,    204, 1535185544 , 'john'     ,   'paulo'    ,   0),
(3  ,   7 ,    204, 1536667819 , 'jenny'    ,   'patrick'  ,   0),
(4  ,   6 ,    204, 1538384821 , 'koma'     ,   'mahaj'    ,   1);

SELECT *
FROM `tbl_main` `m`
JOIN `tbl_info` `i` ON `i`.`main_id` = `m`.`id`
WHERE ((`i`.`operationstatus` = '0'AND `i`.`epochtime` > '0')OR (`i`.`operationstatus` = '1' AND `i`.epochtime = (SELECT MAX(epochtime) FROM tbl_info)))
AND `did` = '204'
GROUP BY `m`.`id`
ORDER BY `i`.`epochtime` DESC  ;

+------+--------+------+---------+------+------------+-----------+----------+-----------------+
| id   | number | id   | main_id | did  | epochtime  | firstname | lastname | operationstatus |
+------+--------+------+---------+------+------------+-----------+----------+-----------------+
|    6 |  85528 |    4 |       6 |  204 | 1538384821 | koma      | mahaj    |               1 |
|    7 |  98236 |    3 |       7 |  204 | 1536667819 | jenny     | patrick  |               0 |
|    5 |  98236 |    2 |       5 |  204 | 1535185544 | john      | paulo    |               0 |
+------+--------+------+---------+------+------------+-----------+----------+-----------------+
3 rows in set (0.00 sec)

这是一个dbfiddle,它也产生3行https://www.db-fiddle.com/f/9N9A25zfpki8QqyRecduem/0