3

我目前有一个搜索功能,我需要它从不同的表中返回值(如果值存在)

SELECT crm.lead_id, 
       crm.id, 
       url, 
       contact, 
       info, 
       status_id, 
       added, 
       last_edited, 
       callback_date, 
       user.user, 
       status.status, 
       crm_updates.status_info
  FROM crm, 
       user, 
       status, 
       crm_updates
 WHERE (url LIKE  '%$search%'
      OR contact LIKE  '%$search%'
      OR info LIKE  '%$search%'
      OR status_id LIKE  '%$search%'
      OR added LIKE  '%$search%'
      OR last_edited LIKE  '%$search%'
      OR callback_date LIKE  '%$search%'
      OR user.user LIKE  '%$search%')
  AND crm.lead_id = user.id
  AND status.id = crm.status_id
  AND crm_updates.crm_id = crm.id

我需要来自 crm_updates 的 status_info,它们当前通过 crm_id(在 crm_updates 中)和 id(在 crm 中)连接

现在只选择具有 status_info 的值。我需要选择所有值并包含 status_info(如果存在)。

4

2 回答 2

2
SELECT crm.lead_id, crm.id, url, contact, info, status_id, added, last_edited, callback_date, user.user, status.status, crm_updates.status_info
FROM crm
INNER JOIN user
ON crm.lead_id = user.id
INNER JOIN status
ON crm.status_id = status.id
LEFT JOIN crm_updates
ON crm.id = crm_updates.crm_id 
WHERE url LIKE  '%$search%'
OR contact LIKE  '%$search%'
OR info LIKE  '%$search%'
OR status_id LIKE  '%$search%'
OR added LIKE  '%$search%'
OR last_edited LIKE  '%$search%'
OR callback_date LIKE  '%$search%'
OR user.user LIKE  '%$search%'

使用 'LEFT JOIN crm_updates':所以如果没有 crm_updates 的记录,将返回 NULL。

提示:不要将连接子句放在查询的 where 部分

于 2013-08-23T11:06:27.770 回答
0

换线怎么样

AND status.id = crm.status_id

AND status.id =* crm.status_id

于 2013-08-23T11:02:47.667 回答