2

私はこのテーブルを持っています:

id type otherid
1   4     1234
2   5     1234
3   4     4321

ご覧のとおり、3つのレコードがあり、そのうちの2つはotherid "1234"に属しており、タイプは4と5です。

最後のレコードは「4321」のotheridに属し、タイプは4のみです。

タイプ5ではなくタイプ4のみを取得した他のすべてのIDを選択する必要があります。

例:このテーブルで選択した後、クエリはレコード3のみを返す必要があります

ありがとう

add1:

TYPEは1から20までの任意の数であると考えてください。タイプ4を取得したotheridのみが必要であり、タイプ5は必要ありません(他のタイプを使用できる場合を除く)。

add2:

mysql5.1を使用する

4

3 に答える 3

1

not existsサブクエリを使用できます。

select  distinct otherid
from    YourTable as yt1
where   yt1.type = 4
        and not exists
        (
        select  *
        from    YourTable as yt2
        where   yt1.otherid = yt2.otherid
                and yt1.type <> yt2.type  -- use this line for any difference
                and yt2.type = 5          -- or this line to just exclude 5
        )
于 2011-02-20T21:25:38.677 に答える
1

これは一種の回避策です

SELECT * FROM (
  SELECT GROUP_CONCAT('|',type,'|') type,other_id FROM table GROUP BY otherid
) t WHERE type LIKE '%|4|%' AND type NOT LIKE '%|5|%'
于 2011-02-20T21:31:22.713 に答える
0

もう1つの方法は、タイプ4と5の両方を持つ行を除外する場所から左結合を使用することです。

select a.*
from table1 a
    left join table1 b on b.otherid = a.otherid and b.type = 5
where a.type = 4 and b.id is null
于 2011-02-20T21:50:22.313 に答える