mysql连接问题

时间:2011-07-07 10:33:31

标签: mysql sql join

研讨会和类别表之间有很多关系。

我有3张桌子: 研讨会,类别,category_workshop_tie。

在category_workshop_tie中,我有工作室和类别的ID。

category_workshop_tie:

category_id | workshop_id

车间:

id | title

类别:

id | name

我有代码:

$this->db->query('
    SELECT workshop.*, tie.category_id 
    FROM `prefix_workshop` workshop, `prefix_category_workshop_tie` tie
    WHERE ((workshop.title LIKE ? OR workshop.descr LIKE ?) AND workshop.city LIKE ? AND workshop.state LIKE ?) AND (workshop.id = tie.workshop_id)
    ORDER BY `d_course`', '%'.($d['f'] ? $d['f'] : '' ).'%', '%'.($d['f'] ? $d['f'] : '' ).'%', '%'.($d['city'] != '0' ? str_replace('_', ' ', $d['city']) : '').'%', '%'.($d['state'] != '0' ? str_replace('_', ' ', $d['state']) : '').'%');

$ d等于$ _GET;
QUERY STRING:f =& city = Odessa& state = Odesska_Region& category = 0& workshop_search_submit = Submit 输出示例:

Array ( [id] => 11 [title] => Hello worldicos [presenter] => Bill [hours] => 5.00 [d_course] => 2011-07-06 [d_course_end] => 0000-00-00 [location] => [city] => Odessa [state] => Odesska Region [cost] => 53.00 [descr] => dwqhhd whqdhwq yhhd qywhdy hyqh d [sponsor] => Microshka [contact] => Mirgorod [website] => http://mirgorod.us [dt_update] => 2011-07-07 08:44:15 [user_id] => 1 [addr] => Kominterna street 10 A [image] => [category_id] => 6 ) Array ( [id] => 11 [title] => Hello worldicos [presenter] => Bill [hours] => 5.00 [d_course] => 2011-07-06 [d_course_end] => 0000-00-00 [location] => [city] => Odessa [state] => Odesska Region [cost] => 53.00 [descr] => dwqhhd whqdhwq yhhd qywhdy hyqh d [sponsor] => Microshka [contact] => Mirgorod [website] => http://mirgorod.us [dt_update] => 2011-07-07 08:44:15 [user_id] => 1 [addr] => Kominterna street 10 A [image] => [category_id] => 9 ) Array ( [id] => 11 [title] => Hello worldicos [presenter] => Bill [hours] => 5.00 [d_course] => 2011-07-06 [d_course_end] => 0000-00-00 [location] => [city] => Odessa [state] => Odesska Region [cost] => 53.00 [descr] => dwqhhd whqdhwq yhhd qywhdy hyqh d [sponsor] => Microshka [contact] => Mirgorod [website] => http://mirgorod.us [dt_update] => 2011-07-07 08:44:15 [user_id] => 1 [addr] => Kominterna street 10 A [image] => [category_id] => 17 ) Array ( [id] => 11 [title] => Hello worldicos [presenter] => Bill [hours] => 5.00 [d_course] => 2011-07-06 [d_course_end] => 0000-00-00 [location] => [city] => Odessa [state] => Odesska Region [cost] => 53.00 [descr] => dwqhhd whqdhwq yhhd qywhdy hyqh d [sponsor] => Microshka [contact] => Mirgorod [website] => http://mirgorod.us [dt_update] => 2011-07-07 08:44:15 [user_id] => 1 [addr] => Kominterna street 10 A [image] => [category_id] => 1 ) Array ( [id] => 11 [title] => Hello worldicos [presenter] => Bill [hours] => 5.00 [d_course] => 2011-07-06 [d_course_end] => 0000-00-00 [location] => [city] => Odessa [state] => Odesska Region [cost] => 53.00 [descr] => dwqhhd whqdhwq yhhd qywhdy hyqh d [sponsor] => Microshka [contact] => Mirgorod [website] => http://mirgorod.us [dt_update] => 2011-07-07 08:44:15 [user_id] => 1 [addr] => Kominterna street 10 A [image] => [category_id] => 4 ) Array ( [id] => 11 [title] => Hello worldicos [presenter] => Bill [hours] => 5.00 [d_course] => 2011-07-06 [d_course_end] => 0000-00-00 [location] => [city] => Odessa [state] => Odesska Region [cost] => 53.00 [descr] => dwqhhd whqdhwq yhhd qywhdy hyqh d [sponsor] => Microshka [contact] => Mirgorod [website] => http://mirgorod.us [dt_update] => 2011-07-07 08:44:15 [user_id] => 1 [addr] => Kominterna street 10 A [image] => [category_id] => 5 ) Array ( [id] => 11 [title] => Hello worldicos [presenter] => Bill [hours] => 5.00 [d_course] => 2011-07-06 [d_course_end] => 0000-00-00 [location] => [city] => Odessa [state] => Odesska Region [cost] => 53.00 [descr] => dwqhhd whqdhwq yhhd qywhdy hyqh d [sponsor] => Microshka [contact] => Mirgorod [website] => http://mirgorod.us [dt_update] => 2011-07-07 08:44:15 [user_id] => 1 [addr] => Kominterna street 10 A [image] => [category_id] => 7 ) Array ( [id] => 11 [title] => Hello worldicos [presenter] => Bill [hours] => 5.00 [d_course] => 2011-07-06 [d_course_end] => 0000-00-00 [location] => [city] => Odessa [state] => Odesska Region [cost] => 53.00 [descr] => dwqhhd whqdhwq yhhd qywhdy hyqh d [sponsor] => Microshka [contact] => Mirgorod [website] => http://mirgorod.us [dt_update] => 2011-07-07 08:44:15 [user_id] => 1 [addr] => Kominterna street 10 A [image] => [category_id] => 8 ) Array ( [id] => 11 [title] => Hello worldicos [presenter] => Bill [hours] => 5.00 [d_course] => 2011-07-06 [d_course_end] => 0000-00-00 [location] => [city] => Odessa [state] => Odesska Region [cost] => 53.00 [descr] => dwqhhd whqdhwq yhhd qywhdy hyqh d [sponsor] => Microshka [contact] => Mirgorod [website] => http://mirgorod.us [dt_update] => 2011-07-07 08:44:15 [user_id] => 1 [addr] => Kominterna street 10 A [image] => [category_id] => 13 )

我每行打印一次。

问题是我从这个查询得到:(workshop_matched_item)*本次研讨会的所有类别。

例如:如果一个研讨会匹配,这个研讨会有5个类别,那么我得到5行。但是我需要一行,有5个类别的研讨会。

我做错了什么?

2 个答案:

答案 0 :(得分:2)

workshop.id分组并使用GROUP_CONCAT()的{​​{1}}功能:

tie.category_id

有关GROUP_CONCAT()函数的详细信息,请参阅MySQL文档,例如,如果您想要一个不同的分隔符而不是逗号,或者您想要在列表中定义SELECT workshop.*, GROUP_CONCAT(tie.category_id) AS category_id FROM `prefix_workshop` workshop, `prefix_category_workshop_tie` tie WHERE ( (workshop.title LIKE ? OR workshop.descr LIKE ?) AND workshop.city LIKE ? AND workshop.state LIKE ?) AND (workshop.id = tie.workshop_id) GROUP BY workshop.id ORDER BY ... 的顺序。


注意:用于连接两个表的category_id语法非常陈旧。尝试开始使用显式WHERE代替:

JOIN

除了在JOINed表附近具有JOIN条件并与WHERE条件分开的优点之外,它也很有用,因为在许多情况下(如此),来自一个表(车间)的行可能与任何行无关在第二个表(一个类别)中。这些研讨会不会与您拥有的代码一起显示(上述JOIN示例也不会显示)。

但你可能仍想展示它们。使用SELECT workshop.*, GROUP_CONCAT(tie.category_id) AS category_id FROM `prefix_workshop` workshop JOIN `prefix_category_workshop_tie` tie ON workshop.id = tie.workshop_id WHERE (workshop.title LIKE ? OR workshop.descr LIKE ?) AND workshop.city LIKE ? AND workshop.state LIKE ? GROUP BY workshop.id ORDER BY ... 语法会变得复杂。使用WHERE语法,您只需将JOIN更改为JOIN(因此,LEFT JOIN关系中LEFT表中的所有列都包含在内,而不仅仅是相关的列。< / p>

答案 1 :(得分:1)

目前还不完全清楚您希望获得哪种格式的结果。

您可以使用MySQL GROUP_CONCAT()方法将所有类别ID组合成一个字符串,也许以逗号分隔。

但是,我认为,如果您要将类别ID用于显示以外的任何内容,那么仅针对ID执行第二个查询会更简单,更有用 - 就像这样:

SELECT category_id
FROM prefix_category_workshop_tie
WHERE workshop_id = $workshopId

如果您要使用类别ID查询类别表,那么您不需要在中间存储ID,您可以执行以下操作:

SELECT category_id category.somevalue
FROM category_workshop_tie
  WHERE workshop_id = $workshopId
LEFT JOIN category
  ON category.id = category_id

这两个假设$workshopId已经从第一个查询设置。