1

我正在尝试查询网站搜索。我需要对这个查询做的是我想根据搜索关键字(导师、机构、学科、城市)选择四个主要列。键入关键字时,查询必须检查这四个表,并且需要在这四个类别下显示搜索结果。显示搜索结果时,我需要添加一些值以生成有意义的搜索结果。例如:考虑在导师类别下显示导师作为搜索结果,我需要显示他/她的姓名、城市、个人资料图片等。

它是这样的。

**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% 工作。问题是在机构类别下不显示城市名称和头像,在城市和学科字段下无法显示地区名称和类别名称。而且我想这种查询是无效的,是否浪费时间和资源?

谢谢你。

4

2 回答 2

0

尝试这个:

SELECT keyword, col, city_name, image_name, tutor_code, district_name, category_name 
FROM (
    SELECT tutor_name AS keyword, 'Tutors' AS col, IFNULL(c1.city_name, '') city_name,  
             IFNULL(ti.image_name, '') image_name, tutor_code, '' district_name, '' category_name 
    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, 
             '' district_name, c.category_name 
    FROM subjects s 
    LEFT JOIN category_subjects cs ON s.subject_id = cs.subject_id 
    LEFT JOIN category c ON cs.category_id = c.category_id 
    UNION
    SELECT city_name AS keyword, 'City' AS col, '' city_name, '' image_name, '' tutor_code, 
             district_name, '' category_name 
    FROM city 
    UNION
    SELECT institute_name AS keyword, 'Institute' AS col, IFNULL(c2.city_name, '') city_name, 
             IFNULL(ii.image_name, '')  image_name, institute_code tutor_code, '' district_name, '' category_name
    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
于 2013-02-10T06:34:09.013 回答
0

UNION尝试将所有SELECT查询的结果放入具有一组列名/数据类型的大表中。因此,所有语句的列数和每列的数据类型SELECT必须匹配。您已经在使用关键字,这是一种区分类型的有用策略。我认为您可以在正确的列中选择额外的数据来完成这项工作。

我认为您应该做的是尝试并衡量性能。然后并行进行单独的数据库调用并获取结果并衡量该性能。我认为它们将与更易于理解和维护的第二个相媲美。

于 2013-02-10T05:48:47.647 回答