基本上我有一个搜索字段来检查多个表。我的问题是,当一个项目没有相关人员时,它不会返回该项目的结果。除此之外它很好。我意识到问题出在WHERE中,要求它只找到与其他表匹配的记录,但我不确定如何纠正这个问题。
谢谢!
$q = "SELECT DISTINCT item.*
FROM item, item_people, people
WHERE item.record_id = item_people.item_id AND people.record_id = item_people.people_id
AND
item.live = '1'
AND
(concat_ws(' ',people.name_first,people.name) LIKE '%$search_param%' OR
item.name_title LIKE '%$search_param%' OR
item.city = '$search_param' OR
item.category LIKE '%$search_param%' OR
item.on_lists LIKE '%$search_param%')
$limit";
答案 0 :(得分:1)
您需要OUTER JOIN
才能返回没有相关人员的项目。
SELECT DISTINCT item.* /*But don't use *!
*/
FROM item
LEFT OUTER JOIN item_people
ON item.record_id = item_people.item_id
LEFT OUTER JOIN people
ON people.record_id = item_people.people_id
WHERE item.live = '1'
AND
(
concat_ws(' ',people.name_first,people.name) LIKE '%$search_param%'
OR item.name_title LIKE '%$search_param%'
OR item.city = '$search_param'
OR item.category LIKE '%$search_param%'
OR item.on_lists LIKE '%$search_param%'
)
$limit