使用JOIN进行正确的查询

时间:2012-09-04 15:07:03

标签: sql

我真的不知道怎么查询这个。我在我的DB中有以下

id,employee,jobsite1,stime,etime,lunchout,lunchin,description,materials,jobsite2,stime,etime,lunchout2,lunchin2,description2,materials2

实际上重复了5次。当我从表格中选择SELECT *作为一个魅力拉取我的所有数据(我将其导出到xls中进行编辑),然后我从jobsite2,3,4和& 5获取所有信息并将其复制并粘贴到ONE行。所以基本上

ID, EMPLOYEE, JOBSITE,  , STIME,ETIME,LUNCHOUT,LUNCHIN, DESCRIPTION, MATERIALS
1,  CHRIS   , MYSITE,   , 9:00, 1:00 ,12:15,   12:45  , WORKED HERE, NONE
1,  CHRIS   , SECONDSITE, 1:15, 5:00 ,        ,       , WORKED HERE, NONE
2,  BOB     , MYSITE    , 9:00, 5:00 ,12:00   ,12:30  , WORKED HERE, NONE
3,  MIKE    , MYSITE    , 9:00, 2:00 ,12:00   ,12:30  , WORKED HERE, BOUGHT STUFF
3,  MIKE    , SECONDSITE, 2:00, 5:30 ,        ,       , DOING THIS , NONE

有没有办法在不通过excel手动执行此操作的情况下查询此问题?使用也许加入?我从来没有使用过JOIN,但是听说过它,并且总是很好奇,一旦我有正确的查询设置,导出这可能会更容易吗?这有意义吗?

1 个答案:

答案 0 :(得分:3)

如果我理解正确,您可以使用UNION

执行此操作
SELECT  id, employee, jobsite1, stime1, etime1, 
        lunchout1, lunchin1, description1, materials1
FROM tableName
UNION
SELECT  id, employee, jobsite2, stime2, etime2, 
        lunchout2, lunchin2, description2, materials2
FROM tableName
UNION
SELECT  id, employee, jobsite3, stime3, etime3, 
        lunchout3, lunchin3, description3, materials3
FROM tableName
UNION
SELECT  id, employee, jobsite4, stime4, etime4, 
        lunchout4, lunchin4, description4, materials4
FROM tableName
SELECT  id, employee, jobsite5, stime5, etime5, 
        lunchout5, lunchin5, description5, materials5
FROM tableName