根据公司ID从左表中使用加入和最后一次插入记录获取数据,并且右表中的所有记录并不重要,因为它具有任何匹配记录左表。
**company_list**
id
company_id
company_name
company_address1
company_address2
company_phone
company_headoffice
active
**company_subscription_list** table field name is
id
company_id
company_name
company_address1
company_address2
company_phone
company_headoffice
active
我正在使用此查询进行fecth记录
SELECT *, c.id AS main_id
FROM company_list as c
LEFT JOIN company_subscription_list as s ON c.company_id = s.company_id
where s.id IN (select max(id) from company_subscription_list GROUP BY company_id)
但它没有显示那些尚未订阅的公司的结果。
需要帮助。
答案 0 :(得分:1)
你必须在条件中加入条件。这样做,你不会丢失线条:
SELECT *, c.id AS main_id
FROM company_list as c
LEFT JOIN company_subscription_list as s
ON c.company_id = s.company_id
AND s.id IN (select max(id) from company_subscription_list GROUP BY company_id)