Return 2 rows from 2nd table INNER JOIN

时间:2017-08-05 12:30:53

标签: mysql sql

I've 2 tables, crm_lead and statussen.

This is (part of) content table statussen

stat_id   | stat_type       | stat_naam 
1         |   lead_status   |   Ingevoerd CJV     
2         |   lead_status   |   Ingevoerd website
...       |   ...           |   ...    
11        |   lead_bron     |   Zelf gegenereerd
12        |   lead_bron     |   Bestaande klant

In lead i've 1 column "lead_status" and "lead_bron". Both have the id of the corresponding row from table statussen.

When I run following query:

SELECT * FROM crm_lead
INNER JOIN statussen on crm_lead.lead_status=statussen.stat_id
WHERE lead_id=31

The result is only showing one row of table statussen (lead_status) and not lead_bron...

I get the single row from table crm_lead with lead_status = 1 and lead_bron = 11. But I also get only 1 row from table statussen: stat_id = 1. But I also need the row with stat_id = 11 from that table..

1 个答案:

答案 0 :(得分:0)

这是因为您只搜索lead_status而不是lead_bron(在您的加入中)。

这是获得您正在寻找的内容的一种方式:

SELECT *
FROM
    crm_lead
    INNER JOIN statussen ON crm_lead.lead_status=statussen.stat_id
WHERE lead_id=31

UNION ALL

SELECT *
FROM
    crm_lead
    INNER JOIN statussen ON crm_lead.lead_bron=statussen.stat_id
WHERE lead_id=31