MySql - LEFT JOIN首选项但没有重复项

时间:2016-03-08 02:04:48

标签: mysql duplicates left-join

所以我必须将表连接到另一个表中的值,但是它应该只从连接表中返回1个值 这些表的关系非常模糊 - 我正在处理的数据的性质也是如此。 “' Cookie''中的名称表格可以是CookieAttr1CookieAttr2CookieAttr3之一。

Cookies
|-----------------------------|
| CookieName  | CookieValue   |
|-------------+---------------|
| Choc-Chip   | 1200          |
| Dough       | 500           |
|-----------------------------|

CookieColor
|-----------------------------------------------------------------|
| Id  | CookieAttr1   | CookieAttr2   | CookieAttr3   | Color     |
|-----+---------------+---------------+---------------+-----------|
| 1   |               | Choc-Chip     | Dough         | Red       |
| 2   |               | Choc-Chip     | Crumbs        | Orange    |
| 3   |               | Sultanas      | Dough         | Red       |
| 4   |               | Choc-Chip     | Dough         | Blue      |
| 5   | Dough         |               | Rounded       | Purple    |
| 6   | Dough         | Big           | Rounded       | Green     |
| 7   | Rounded       |               | Rounded       | Pink      |
|-----------------------------------------------------------------|

到目前为止,该表已按首选项加入 - 如果CookieAttr1为空,则会在CookieAttr2上加入,然后CookieAttr3(将始终具有值)

SELECT 
    CookieName,
    CookieValue,
    Color
FROM Cookies
LEFT OUTER JOIN (
    SELECT 
        Color,
        CookieAttr1,
        CookieAttr2,
        CookieAttr3
    FROM CookieColor
    GROUP BY CookieAttr3
) CookieColor
    ON COALESCE(CookieAttr1, CookieAttr2, CookieAttr3) = CookieName

出现问题的是,该表可以返回重复项,因为CookieAttr3分组可能会有多个相同的CookieAttr2值。

CookieColor (GROUPED BY `CookieAttr3`)
|-----------------------------------------------------------------|
| Id  | CookieAttr1   | CookieAttr2   | CookieAttr3   | Color     |
|-----+---------------+---------------+---------------+-----------|
| 1   |               | Choc-Chip     | Dough         | Red       |
| 2   |               | Choc-Chip     | Crumbs        | Orange    |
| 5   | Dough         |               | Rounded       | Purple    |
|-----------------------------------------------------------------|

==========> result of query
|-----------------------------------------|
| CookieName  | CookieValue   | Color     |
|-------------+---------------+-----------|
| Choc-Chip   | 1200          | Red       |
| Choc-Chip   | 1200          | Orange    |
| Dough       | 500           | Purple    |
|-----------------------------------------|

==========> Preferred Result
|-----------------------------------------|
| CookieName  | CookieValue   | Color     |
|-------------+---------------+-----------|
| Choc-Chip   | 1200          | Red       |
| Dough       | 500           | Purple    |
|-----------------------------------------|

我怎样才能获得Choc-Chip'

的一个值

1 个答案:

答案 0 :(得分:1)

您始终可以在一个子查询中使用pop(),但您的论坛不清楚

<强> SQL Fiddle Demo

COALESCE

输出

但是不知道你想要什么SELECT CookieAttr, Color, CookieValue FROM ( SELECT `Id`, COALESCE(CookieAttr1, CookieAttr2, CookieAttr3) CookieAttr, `Color` FROM CookieColor ) C JOIN Cookies ON C.CookieAttr = Cookies.CookieName

group by

如果你添加

| CookieAttr |  Color | CookieValue |
|------------|--------|-------------|
|  Choc-Chip | Orange |        1200 |
|  Choc-Chip |   Blue |        1200 |
|  Choc-Chip |    Red |        1200 |
|      Dough | Purple |         500 |
|      Dough |  Green |         500 |

但颜色会随机。

GROUP BY CookieAttr;