0

我遇到了一些问题。我有各种要加入的表,但我还需要获取检索到的每一行的类别。我需要从一个表中检索类别 ID,然后在另一个表中查找类别名称。我认为它更容易展示。这是我失败的 sql 查询尝试:

SELECT     DISTINCT(t.tour_name), tp.tprecio_precio_adulto, 
           tp.tprecio_precio_menor, category_name 
FROM       tour AS t 
INNER JOIN tour_categories AS cc ON tcategoria_fktour = t.tour_id
INNER JOIN tour_precio AS tp ON tp.tprecio_fktour = t.tour_id
LEFT JOIN  categories AS c ON c.category_id = tc.category_id 
WHERE      tp.tprecio_fecha_inicio <= '2012-10-27' 
       AND tp.tprecio_fecha_final >= '2012-10-27' 
       AND t.tour_activo = '1' 
       AND tp.tprecio_precio_adulto != '0.00'

以下是表格布局:

table: tour
tour_id      |    tour_name     
-----------------------------------------
 1                tour name 1
 2                tour name 2
 3                tour name 3

table: tour_categories
  category_id    |    tour_id    
------------------------------------
     1                   3             
     2                   3
     3                   3

 table: categories
      category_id    |    category_name    
    ------------------------------------
          1              category name 1            
          2              category name 2  
          3              category name 3  

如您所见,每次旅行可以有多个类别,我需要获取这些类别并将它们组合在一起,然后为每个类别获取相应的类别名称。我不知道如何做到这一点。任何帮助将不胜感激!提前感谢您的帮助

4

1 回答 1

0

我想通了,以防万一这可以帮助其他人。我在巡演名称上做了一个组连接,并添加了一个分隔符。它现在完美运行:

SELECT GROUP_CONCAT(c.category_name ORDER BY c.category_name ASC SEPARATOR ', ') AS categories, t.tour_name
FROM tour AS t
INNER JOIN tour_categoria AS cc ON cc.category_id = t.tour_id
LEFT OUTER JOIN categoria AS c ON c.category_name_id = cc.category_id
GROUP BY t.tour_name
于 2012-10-28T17:04:34.223 回答