1
問題選擇列是與此類似How to write a MySQL query that returns a temporary column containing flags for whether or not an item related to that row exists in another table基於另一個表中的特定行是否存在
除了我需要更具體的瞭解這行存在
我有兩個表:「比賽」和 'competition_entries'
比賽:
ID | NAME | TYPE
--------------------------------
1 | Example | example type
2 | Another | example type
參賽作品
ID | USERID | COMPETITIONID
---------------------------------
1 | 100 | 1
2 | 110 | 1
3 | 110 | 2
4 | 120 | 1
我想選擇比賽,但添加一個額外的列,指定用戶是否參加比賽。這是我目前的SELECT語句
SELECT
c.[ID],
c.[NAME],
c.[TYPE],
(CASE
WHEN e.ID IS NOT NULL AND e.USERID = @userid THEN 1
ELSE 0
END
) AS 'ENTERED'
FROM competitions AS c
LEFT OUTER JOIN competition_entries AS e
ON e.COMPETITIONID = c.ID
我期望的結果從@userid
參數設置爲110
設置是這樣的
ID | NAME | TYPE | ENTERED
-------------------------------------
1 | Example | example type | 1
2 | Another | example type | 1
而是我得到這個
ID | NAME | TYPE | ENTERED
-------------------------------------
1 | Example | example type | 0
1 | Example | example type | 1
1 | Example | example type | 0
2 | Another | example type | 1
因爲它計算的條目爲所有用戶標識
是的,工作使用第二個,更容易理解,我知道這是沿着這些線。感謝您的命名提示。 –