簡體   English   中英

當一個表中的多行與另一表中的一行相關時,在MySQL中聯接兩個表

[英]JOINing two tables in MySQL when multiple rows in one table relate to one row in another

我有兩個表, module_configmodule_settings 后者包含具有默認值的行,而前者包含將覆蓋默認值的行。

我在此小提琴中創建了帶有一些測試行的表: http ://sqlfiddle.com/#!2/ ad4eb

這是我的規格:

1)我想提供的列表gid秒。 例如,提供100244850 2)我希望查詢為每個gid返回每個設置的行(在module_settings給出)。 它應該忽略任何module_settings排在那里module_settings.gid_specific等於0 3)如果該行存在,則value將等於module_config給定的對應值,否則module_settings給定的initial值。

有了上面的小提琴,我想要可以返回以下內容的東西(用於輸入上述4個gid值):

| mid----- | gid | name-------- | value |
| currency | 100 | hand_default | 12--- |
| currency | 24- | hand_default | 17--- |
| currency | 48- | hand_default | 29--- |
| currency | 50- | hand_default | 0---- |
| currency | 100 | bank_default | 0---- |
| currency | 24- | bank_default | 0---- |
| currency | 48- | bank_default | 0---- |
| currency | 50- | bank_default | 0---- |

因此,它首先在module_settings找到gid_specific等於1的所有行。這些行通過具有midname的唯一組合而不同。 之后,針對每個gid每個設置返回一行。 所以對於module_setting其中排mid等於currencyname等於hand_default ,它將返回4行,每一個gid100244850 剩下要做的唯一事情就是確定要與此行關聯的值。 如果module_config中存在一行,其中module_config.sid等於module_settings.idmodule_config.gid是所討論的gid ,則使用module_config.value給定的值,否則使用module_settings.initial給定的默認值。

如您所見,我已經把它放在腦子里了,但我只是不敢將其放入查詢語句中。 如果有人能闡明一點,我將非常感激。

我已經使用過Teradata SQL。 我敢肯定,如果您嘗試這樣做,則可以找到SQL的等效語法。

除了您的兩個表之外,我還使用了tbl3作為您所說的來自PHP數組的gid的來源。

步驟1:交叉連接module_settingsmodule_config

SELECT mid,gid,name, b.value1
FROM IBB_APP.module_settings a, IBB_APP.module_config b
WHERE gid_specific = 1;

結果是

mid                                       gid  name                              value1
--------------------------------  -----------  --------------------------------  ----------
currency                                  100  hand_default                      12
currency                                   24  hand_default                      17
currency                                   48  hand_default                      29
currency                                  100  bank_default                      12
currency                                   24  bank_default                      17
currency                                   48  bank_default                      29

步驟2.包括tbl3 ,它也給出了gid = 50。

SELECT mid,c.gid,name, 
    CASE WHEN b.gid = c.gid THEN b.value1
         WHEN b.gid IS NULL THEN a.initial1 END AS value1
FROM IBB_APP.module_settings a, IBB_APP.module_config b
RIGHT OUTER JOIN IBB_APP.tbl3 c ON (b.gid = c.gid)
WHERE gid_specific = 1
ORDER BY name,c.gid;

結果是

mid                                       gid  name                              value1
--------------------------------  -----------  --------------------------------  ----------
currency                                   24  bank_default                      17
currency                                   48  bank_default                      29
currency                                   50  bank_default                      0
currency                                  100  bank_default                      12
currency                                   24  hand_default                      17
currency                                   48  hand_default                      29
currency                                   50  hand_default                      0
currency                                  100  hand_default                      12

希望對您有所幫助,並且我能正確理解您的要求。

暫無
暫無

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

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