需要查询逻辑,如何按用户表中的id分组?

时间:2018-03-06 06:29:21

标签: mysql

我有两张桌子:

INSERT INTO `companies` (`name`) VALUES
('Walmart'),
('Disney'),
('Amazon'),
('Unicom'),
('Microsoft'),
('Intel')

INSERT INTO `users` (`id`, `company`) VALUES
(1, 'Disney'),
(2, 'Amazon'),
(3, 'Intel'),
(3, 'Walmart'),
(4, 'Microsoft'),
(4, 'Unicom'),
(5, 'Microsoft')

结果应如下:

1. 'Walmart', 'Amazon', 'Unicom', 'Microsoft', 'Intel'
2. 'Walmart', 'Disney', 'Unicom', 'Microsoft', 'Intel'
3. 'Disney', 'Amazon', 'Unicom', 'Microsoft'
4. 'Walmart', 'Disney', 'Amazon', 'Intel'
5. 'Walmart', 'Disney', 'Amazon', 'Unicom', 'Intel'

我尝试过:

"SELECT a.name, b.id, b.company FROM users RIGHT JOIN companies ON b.company <> a.name"

这通过省略列表中已有的公司名称来提供正确的逻辑,但问题是它处理相同的ID两次并省略不同的公司名称。如何处理此查询?

2 个答案:

答案 0 :(得分:2)

以下查询中的基本思想是将包含每个可能的用户/公司组合的日历表连接到users表。 匹配的那些组合将被删除,然后其他公司将使用GROUP_CONCAT汇总为每个用户的CSV字符串。

SELECT t1.id, GROUP_CONCAT(t1.name)
FROM
(
    SELECT DISTINCT u.id, c.name
    FROM users u
    CROSS JOIN companies c
) t1
LEFT JOIN users t2
    ON t1.name = t2.company AND t1.id = t2.id
WHERE t2.company IS NULL
GROUP BY
    t1.id;

enter image description here

Demo

答案 1 :(得分:0)

试试这个

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CFrameWndEx::OnCreate(lpCreateStruct) == -1)
        return -1;

    BOOL bNameValid;

    // create a view to occupy the client area of the frame
    if (!m_wndView.Create(NULL, NULL, AFX_WS_DEFAULT_VIEW, CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL))
    {
        TRACE0("Failed to create view window\n");
        return -1;
    }

    m_wndRibbonBar.Create(this);
    m_wndRibbonBar.LoadFromResource(IDR_RIBBON);

    if (!m_wndStatusBar.Create(this))
    {
        TRACE0("Failed to create status bar\n");
        return -1;      // fail to create
    }

    CString strTitlePane1;
    CString strTitlePane2;
    bNameValid = strTitlePane1.LoadString(IDS_STATUS_PANE1);
    ASSERT(bNameValid);
    bNameValid = strTitlePane2.LoadString(IDS_STATUS_PANE2);
    ASSERT(bNameValid);
    m_wndStatusBar.AddElement(new CMFCRibbonStatusBarPane(ID_STATUSBAR_PANE1, strTitlePane1, TRUE), strTitlePane1);
    m_wndStatusBar.AddExtendedElement(new CMFCRibbonStatusBarPane(ID_STATUSBAR_PANE2, strTitlePane2, TRUE), strTitlePane2);

    // enable Visual Studio 2005 style docking window behavior
    CDockingManager::SetDockingMode(DT_SMART);
    // enable Visual Studio 2005 style docking window auto-hide behavior
    EnableAutoHidePanes(CBRS_ALIGN_ANY);

    return 0;
}


BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/,
    CCreateContext* pContext)
{
    return m_wndSplitter.Create(this,
        2, 2,               // TODO: adjust the number of rows, columns
        CSize(10, 10),      // TODO: adjust the minimum pane size
        pContext);
}