我目前有一个搜索功能,我需要它来从不同的表中返回值(如果值存在)
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。
答案 0 :(得分: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。
提示:不要将join子句放在查询的where部分
答案 1 :(得分:0)
如何更改行
AND status.id = crm.status_id
到
AND status.id = * crm.status_id