如何在mysql中按日期从另一个表中拉出相同的行

时间:2012-03-03 23:04:10

标签: mysql sql

我有两张桌子。

first table 

    f_id | date    |
    --------------
    1    |2012-1-01
    1    |2012-1-02

    second table 

    s_id | f_id | name
    -------------------
    1    | 1    |rakib 
    2    | 1    | shohug

我想查看这些数据 喜欢

f_id| date     | s_id | name
--------------------------
1   |2012-1-01 | 1    | rakib 
1   |2012-1-01 | 2    | shohug
1   |2012-1-02 | 1    | rakib 
1   |2012-1-02 | 2    | shohug

plz帮我如何在mysql查询中编写它

3 个答案:

答案 0 :(得分:1)

select f.f_id, f.`date`, s.s_id, s.`name`
from firsttable f
join secondtable s on f.f_id=s.f_id
order by f.f_id, f.`date`, s.s_id

答案 1 :(得分:0)

你的意思是

SELECT first.f_id, first.date, second.s_id, second.name
  FROM first JOIN second ON second.f_id = first.f_id

(见http://en.wikipedia.org/wiki/Join_(SQL)。)

答案 2 :(得分:0)

似乎你想要做FULL OUTER加入它包括来自两个表的所有行。 Mysql不直接支持它。

但你可以尝试下面的内容:

select * from firsttable  as a
   left outer join secondtable as o on a.f_id = o.f_id
union all
select * from firsttable as a
   right outer join secondtable as o on a.f_id = o.f_id

另外,本文将帮助您http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/