SQL在其中一个字段中查找具有相同值的记录

时间:2014-09-05 14:19:58

标签: sql

嘿,我正在尝试识别那些与co72010讲座之一同时开始的事件。而且我不确定如何做到这一点我必须做INNER JOINGROUP BY

events

id          modle kind     dow    tod    duration       room
co12004.L01 co12004 L   Wednesday 11:00     1   cr.SMH
co12004.L02 co12004 L   Monday    17:00     1   cr.B13
co12004.T01 co12004 T   Monday  11:00       2   co.G78+G82
co12004.T02 co12004 T   Tuesday 15:00       2   co.B7
co12004.T03 co12004 T   Tuesday 13:00       2   co.G78+G82

3 个答案:

答案 0 :(得分:1)

您可以使用Exists。也许是这样的:

Select  E.*
From    Events  E
Where Exists
(
    Select  1
    From    Events  O
    Where   O.dow = E.dow
    And     O.tod = E.tod
    And     O.modle = 'co72010'
)

或者,您也可以使用Join

Select  E.*
From    Events  E
Join    Events  O   On  O.Dow = E.Dow
                    And O.Tod = E.Tod
Where   O.Modle = 'co72010'

答案 1 :(得分:1)

Select  E.*
From    event  E
Where Exists
(
    Select  1
    From    event  O
    Where   O.dow = E.dow
    And     O.tod = E.tod
    And     O.modle = 'co72010'
) and E.modle != 'co72010'

因此它不会显示相同的模块,只显示与co72010同时发生的模块,而不是它本身?

答案 2 :(得分:0)

SELECT a.id, a.modle, a.dow, a.tod, b.id, b.modle, b.dow, b.tod
FROM event a JOIN event b on (a.dow=b.dow AND a.tod=b.tod)
WHERE a.modle = 'co72010' AND b.modle != 'co72010'