-1

这是一个选择查询...

   $q = "SELECT
        tcs.tutor_id AS tid,
        tcs.category_id AS cid,
        tcs.subject_id AS sid,
        GROUP_CONCAT( DISTINCT s.subjects SEPARATOR ', ') AS subjects,
        t.tutor_name AS tname,
        t.tutor_code AS tcode,
        DATE_FORMAT(t.registration_date, '%b %D, %Y') AS date,
        t.qualification AS qualifi,
        GROUP_CONCAT( DISTINCT o.option_name SEPARATOR ', ') AS tutor_option,
        timg.image_name AS img,
        city_name AS city,
        d.district_name AS district
   FROM tutor_category_subject as tcs
        INNER JOIN subject AS s ON tcs.subject_id = s.subject_id
        INNER JOIN tutor_option AS toption ON toption.tutor_id = tcs.tutor_id
        INNER JOIN options AS o ON toption.option_id = o.option_id
        INNER JOIN tutors AS t ON tcs.tutor_id = t.tutor_id
        INNER JOIN address ON address.address_id = t.address_id
        INNER JOIN city ON city.city_id = address.city_id
        INNER JOIN district AS d ON d.district_id = city.district_id
        LEFT JOIN tutor_images AS timg ON timg.tutor_id = tcs.tutor_id AND timg.image_type = 'profile'
   WHERE s.subjects LIKE '%$subject%'
   GROUP BY tcs.tutor_id";

这个查询现在正在工作..现在我发现了一个问题。它是.....这么多科目可能需要一位特定的导师。我需要使用搜索关键字选择与导师相关的所有主题以显示搜索结果。在此查询中,搜索关键字是“$subject”。此查询仅选择与给定关键字相似的导师主题。例如,如果用户给出诸如“商业”之类的关键字,则该查询选择该导师的主题,例如“仅商业研究、商业会计等”。所以我也需要通过这个查询选择其他主题..有人可以帮助我吗?

谢谢你。

4

1 回答 1

0

好吧,我不是 MySQL 大师,但我认为您不需要选择

 tcs.category_id AS cid,
 tcs.subject_id AS sid,

你还需要使用distinct关键字

SELECT distinct
    tcs.tutor_id AS tid,
    GROUP_CONCAT( DISTINCT s.subjects SEPARATOR ', ') AS subjects,
    t.tutor_name AS tname,
    t.tutor_code AS tcode,
    DATE_FORMAT(t.registration_date, '%b %D, %Y') AS date,
    t.qualification AS qualifi,
    GROUP_CONCAT( DISTINCT o.option_name SEPARATOR ', ') AS tutor_option,
    timg.image_name AS img,
    city_name AS city,
    d.district_name AS district
FROM tutor_category_subject as tcs
    INNER JOIN subject AS s ON tcs.subject_id = s.subject_id
    INNER JOIN tutor_option AS toption ON toption.tutor_id = tcs.tutor_id
    INNER JOIN options AS o ON toption.option_id = o.option_id
    INNER JOIN tutors AS t ON tcs.tutor_id = t.tutor_id
    INNER JOIN address ON address.address_id = t.address_id
    INNER JOIN city ON city.city_id = address.city_id
    INNER JOIN district AS d ON d.district_id = city.district_id
    LEFT JOIN tutor_images AS timg ON timg.tutor_id = tcs.tutor_id AND timg.image_type = 'profile'
WHERE s.subjects LIKE '%$subject%'
GROUP BY tcs.tutor_id
于 2012-10-31T17:49:12.257 回答