我正在尝试查询网站搜索。我需要对这个查询做的是我想根据搜索关键字(导师、机构、学科、城市)选择四个主要列。键入关键字时,查询必须检查这四个表,并且需要在这四个类别下显示搜索结果。显示搜索结果时,我需要添加一些值以生成有意义的搜索结果。例如:考虑在导师类别下显示导师作为搜索结果,我需要显示他/她的姓名、城市、个人资料图片等。
它是这样的。
**Tutors**
tutor's name
city
profile image
**institutes**
institute's name
city
profile image
**subjects**
subject name
category name which belong this subject
**cities**
city name
district name which city belong etc...
我试过这样的东西
$q = "SELECT keyword, col, city_name, image_name, tutor_code
FROM (
SELECT tutor_name AS keyword, 'Tutors' AS col, IFNULL(c1.city_name, '') city_name, IFNULL(ti.image_name, '') image_name, tutor_code FROM tutors AS t
LEFT JOIN address a ON t.address_id = a.address_id
LEFT JOIN city c1 ON a.city_id = c1.city_id
LEFT JOIN tutor_images ti ON t.tutor_id = ti.tutor_id AND ti.image_type = 'profile'
UNION
SELECT subject_name AS keyword, 'Subject' AS col, '' city_name, '' image_name, '' tutor_code FROM subjects
UNION
SELECT city_name AS keyword, 'City' AS col, '' city_name, '' image_name, '' tutor_code FROM city
UNION
SELECT institute_name AS keyword, 'Institute' AS col, '' city_name, '' image_name, '' tutor_code FROM institutes AS i
LEFT JOIN address a ON i.address_id = a.address_id
LEFT JOIN city c2 ON a.city_id = c2.city_id
LEFT JOIN institute_images ii ON i.institute_id = ii.institute_id AND ii.image_type = 'profile'
) s
WHERE keyword LIKE '%$queryString%'
LIMIT 10";
这个查询工作大约 50%,但不能 100% 工作。问题是在机构类别下不显示城市名称和头像,在城市和学科字段下无法显示地区名称和类别名称。而且我想这种查询是无效的,是否浪费时间和资源?
谢谢你。