簡體   English   中英

我在MySQL中有4個表-與沒有重復的第4個表聯接(可能為null值)

[英]I have 4 tables in MySQL - Join with 4th table without duplicates (possible null value)

好的,我正在處理4個表,我需要查詢不返回任何重復項,如果需要,則返回null值。 不幸的是,由於我使用的框架存在限制,因此無法使用UNION。 這是我的桌子:

users Table
------------------------------------------------------
id      name
------------------------------------------------------
1       jane
2       john
3       skip


songs Table
------------------------------------------------------
id      artist      title
------------------------------------------------------
1       devo        whip
2       snake       hiss
3       america     great
4       elvis       blues


song_history Table
------------------------------------------------------
singer_id   song_id
------------------------------------------------------
2       1
2       2
2       3
2       4


song_current Table
------------------------------------------------------
singer_id   song_id     event_id
------------------------------------------------------
2       3       20
2       4       22

我已經編寫了兩個查詢,它們可以滿足所需結果的一半,這是第一個查詢:

SELECT name, artist, title
FROM users AS user
LEFT JOIN song_history AS history ON user.id = history.singer_id
LEFT JOIN songs AS song ON history.song_id = song.id 
WHERE user.id = 2

上面的查詢為我提供了john用戶的song_history中所有歌曲的列表:

name    artist      title   
-----------------------------
john  | devo      | whip    |   
john  | snake     | hiss    |   
john  | america   | great   |   

這是所需結果另一半的第二個查詢:

SELECT name, artist, title, event_id
FROM users AS user
LEFT JOIN song_history AS history ON user.id = history.singer_id
LEFT JOIN songs AS song ON history.song_id = song.id
LEFT JOIN song_current AS csongs ON csongs.singer_id = user.id
AND csongs.song_id = history.song_id
WHERE user.id = 2
AND event_id = 20

上面的查詢為我提供了song_history中所有在song_current表中還為event_id為20的歌曲的列表

name    artist      title   event_id
--------------------------------------------
john  | america   | great   |   20

現在,由於我使用的框架,我受到了限制,這限制了我使用UNION,所以我迷失了沒有UNION的方式或方法。

這是我要尋找的結果:

name    artist      title   event_id
-------------------------------------
john  | devo      | whip    |   null
john  | snake     | hiss    |   null
john  | america   | great   |   20
john  | elvis     | blues   |   null

基本上,我需要一個給定用戶在song_history表中的所有歌曲的列表,如果event_id匹配,則需要顯示event_id;如果不匹配,則需要顯示null,但仍顯示在song_history表。

song_history表將始終包含song_current中的內容(沒有event_id),但是,song_current並不總是包含在song_history中找到的數據。

如果可以使用UNION,我將簡單地使用上面兩個成功的查詢,並且可以解決我的問題,但是,由於我不能使用UNION,所以我對LEFT INNER,LEFT OUTER和其他JOINS的所有嘗試都使我望而卻步。

任何幫助是極大的贊賞!

編輯:

請注意,song_current表將具有其他event_id,這將使我無法像這樣使用WHERE / OR:

AND(event_id = 20 OR event_id為NULL);

Joomla 3.3框架最近才包含UNION語句,目前無法在多個列上使用。

這是一個在phpMyAdmin中可以正常工作的UNION查詢的示例:

注意:還有一個名為users.ordering的附加列,該列始終返回0,上面沒有列出-我正在使用它為沒有event_id的歌曲賦予0值。

SELECT name, artist, title, users.ordering AS event_id
FROM users, song_history, song_current
WHERE users.id = song_history.singer_id
AND song_history.song_id = songs.id
AND users.id = '2'
UNION
SELECT name, artist, title, song_current.event_id AS event_id
FROM users, song_current, songs
WHERE users.id = song_current.singer_id
AND song_current.song_id = songs.id
AND users.id = '2'
AND song_current.event_id = '20'

請記住,此UNION查詢有效,但有一個例外(盡管我可以接受),它為event_id為0的用戶列出了song_history中的所有歌曲,然后再次列出了song_history和song_current以及匹配的event_id。 該結果將與上面列出的結果一樣可接受。

您可能需要做的就是刪除event_id上的過濾條件,或者也包括NULL event_id,如下所示

SELECT name, artist, title, 
CASE
  WHEN event_id <> 20 THEN NULL
  ELSE event_id
END event_id
FROM users AS user
LEFT JOIN song_history AS history ON user.id = history.singer_id
LEFT JOIN songs AS song ON history.song_id = song.id
LEFT JOIN song_current AS csongs ON csongs.singer_id = user.id
AND csongs.song_id = history.song_id
WHERE user.id = 2

SQL Fiddle演示第三個查詢包括具有NULL event_id的行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM