使用多个表创建有效查询

时间:2013-02-10 05:40:33

标签: mysql sql select union

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

它是这样的。

**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%工作。问题是在学院类别下不显示城市名称和个人资料图像,不能显示城市和主题领域下的地区名称和类别名称。而且我想这种查询是无效的,是否浪费时间和资源?

谢谢。

2 个答案:

答案 0 :(得分:0)

UNION尝试将所有SELECT个查询的结果放入一个包含一组列名/数据类型的大表中。因此,所有SELECT语句的每列的列数和数据类型必须匹配。您已经在使用关键字,这是一种分离类型的有用策略。我认为您可以在右栏中选择额外的数据来实现这一目标。

我认为你应该做的就是尝试并测量性能。然后并行进行单独的数据库调用并获得结果并测量该性能。我认为它们与第二种相似,更易于理解和维护。

答案 1 :(得分: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