首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mysql内连接查询

Mysql内连接查询
EN

Stack Overflow用户
提问于 2014-05-14 19:50:44
回答 3查看 84关注 0票数 4

我是Mysql查询的新手。

我有两个表(tbl1,tbl2)

我需要用start_time升序连接这两个表,并过滤date和emp_id

此处的表字段不同(start-time -> in-time、end-time -> out-time)

代码语言:javascript
复制
tbl-1:

id      start-time      end-time        date            emp_id

1       09:00:00        09:00:59        2014-05-14        1

2       10:00:00        10:00:59        2014-05-14        1

3       12:00:00        12:00:59        2014-05-14        1

4       14:00:00        14:00:59        2014-05-14        1

5       16:00:00        17:00:59        2014-05-13        1


tbl-2:

id      in-time         out-time        date            emp_id

1       11:00:00        11:00:59        2014-05-14        1

2       13:00:00        13:00:59        2014-05-14        1

3       15:00:00        15:00:59        2014-05-14        1

4       18:00:00        19:00:59        2014-05-14        2

5       20:00:00        20:00:59        2014-05-15        1


filterd by date, emp_id ordered by date

result-tbl:

id      start-time      end-time        date            emp_id

1       09:00:00        09:00:59        2014-05-14        1

2       10:00:00        10:00:59        2014-05-14        1

3       11:00:00        11:00:59        2014-05-14        1

4       12:00:00        12:00:59        2014-05-14        1

5       13:00:00        13:00:59        2014-05-14        1

6       14:00:00        14:00:59        2014-05-14        1

7       15:00:00        15:00:59        2014-05-14        1
EN

回答 3

Stack Overflow用户

发布于 2014-05-14 19:55:58

我不认为你想要加入。我认为您需要一个union all以及一个重新分配id的方法。类似于:

代码语言:javascript
复制
select (@rn := @rn + 1) as id, intime as starttime, outtime as outtime, emp_id
from ((select * from tbl1)
      union all
      (select * from tbl2)
     ) t cross join
     (select @rn := 0) var
where emp_id = 1 and
      intime <= '18:00:00'
order by intime;
票数 1
EN

Stack Overflow用户

发布于 2014-05-14 19:58:36

你想有一个记录的联盟吗?

代码语言:javascript
复制
select in_time, out_time, date, emp_id from tbl1
union all
select star_time, end_time, date, emp_id from tbl2
order by in_time, out_time, date, emp_id;

编辑:加过滤器:

代码语言:javascript
复制
select in_time, out_time, date, emp_id from tbl1
where emp_id = 1 and date = '2014-05-14'
union all
select star_time, end_time, date, emp_id from tbl2
where emp_id = 1 and date = '2014-05-14'
order by in_time, out_time;
票数 0
EN

Stack Overflow用户

发布于 2014-05-14 19:59:16

这可能会解决你的问题。

代码语言:javascript
复制
select *
from (
        select id,star_time,end_time,date,emp_id
        from tbl1
        where [date-filter-condition]
        union all
        select id,in_time as star_time,out_time end_time,date,emp_id
        from tbl2
        where [date-filter-condition]
) tmp
where tmp.date==[some-date-filter] and tmp.emp_id=[some-emp-id-filter]
order by tmp.date
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23653930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档