LEFT JOIN 来自同一行的多行 table | SQL 服务器 2012
LEFT JOIN Multiple rows from the same table | SQL SERVER 2012
我对这个查询有疑问。我不知道如何解决这个问题,因为数据在同一列中,只能用标签 I 和 O 分隔。我将如何处理这个?这可以通过多个 LEFT JOIN 实现吗?
SELECT
CHECKINOUT.userid as chkuserid, USERINFO.userid as usruserid, name,
BADGENUMBER as badge,
LEFT(CONVERT(varchar, CHECKTIME, 23),11) as logindate,checktype,
Format(cast(RIGHT(CHECKTIME,12) as datetime),'HH:mm:ss') AS timestamp
from CHECKINOUT,USERINFO
where CHECKINOUT.userid=USERINFO.userid
and BADGENUMBER = '1693'
这就是查询的样子。
我想要实现的是它看起来像这样。
我可以就此事寻求帮助吗?
这个怎么样。
select t1.*, t2.checktype, t2.timestamp from
(SELECT
CHECKINOUT.userid as chkuserid, USERINFO.userid as usruserid, name,
BADGENUMBER as badge,
LEFT(CONVERT(varchar, CHECKTIME, 23),11) as logindate,checktype,
Format(cast(RIGHT(CHECKTIME,12) as datetime),'HH:mm:ss') AS timestamp
from CHECKINOUT,USERINFO
where CHECKINOUT.userid=USERINFO.userid
and BADGENUMBER = '1693' and checktype = 'I') as t1
left join
(select CHECKINOUT.userid as chkuserid, checktype, LEFT(CONVERT(varchar, CHECKTIME, 23),11) as logindate,
Format(cast(RIGHT(CHECKTIME,12) as datetime),'HH:mm:ss') AS timestamp
from CHECKINOUT,USERINFO
where CHECKINOUT.userid=USERINFO.userid
and BADGENUMBER = '1693' and checktype = '0')) as t2 on t2.chkuserid = t1.chkuserid
and t2.logindate = t1.logindate
我对这个查询有疑问。我不知道如何解决这个问题,因为数据在同一列中,只能用标签 I 和 O 分隔。我将如何处理这个?这可以通过多个 LEFT JOIN 实现吗?
SELECT
CHECKINOUT.userid as chkuserid, USERINFO.userid as usruserid, name,
BADGENUMBER as badge,
LEFT(CONVERT(varchar, CHECKTIME, 23),11) as logindate,checktype,
Format(cast(RIGHT(CHECKTIME,12) as datetime),'HH:mm:ss') AS timestamp
from CHECKINOUT,USERINFO
where CHECKINOUT.userid=USERINFO.userid
and BADGENUMBER = '1693'
这就是查询的样子。
我想要实现的是它看起来像这样。
我可以就此事寻求帮助吗?
这个怎么样。
select t1.*, t2.checktype, t2.timestamp from
(SELECT
CHECKINOUT.userid as chkuserid, USERINFO.userid as usruserid, name,
BADGENUMBER as badge,
LEFT(CONVERT(varchar, CHECKTIME, 23),11) as logindate,checktype,
Format(cast(RIGHT(CHECKTIME,12) as datetime),'HH:mm:ss') AS timestamp
from CHECKINOUT,USERINFO
where CHECKINOUT.userid=USERINFO.userid
and BADGENUMBER = '1693' and checktype = 'I') as t1
left join
(select CHECKINOUT.userid as chkuserid, checktype, LEFT(CONVERT(varchar, CHECKTIME, 23),11) as logindate,
Format(cast(RIGHT(CHECKTIME,12) as datetime),'HH:mm:ss') AS timestamp
from CHECKINOUT,USERINFO
where CHECKINOUT.userid=USERINFO.userid
and BADGENUMBER = '1693' and checktype = '0')) as t2 on t2.chkuserid = t1.chkuserid
and t2.logindate = t1.logindate