简体   繁体   English

无法弄清楚如何从查询中获得想要的结果

[英]Can't figure out how to get result i want from query

I'm new to databases and mysql. 我是数据库和mysql的新手。 I'm having a difficult time even deciding exactly what the problem I'm facing is and don't really know what the relevant google search would be. 我甚至很难确定我要面对的问题是什么,而且我真的不知道相关的Google搜索是什么。 So forgive me if this is a mind-blowingly retarded question. 如果这是一个令人难以置信的智障问题,请原谅我。 I have two tables tabs and cats . 我有两个桌子的tabscats

tabs is like this: tabs是这样的:

name | order
tab1 | 3
tab2 | 0
tab3 | 1
tab4 | 2

cats is like this: cats是这样的:

name | tab_name
cat1 | tab3
cat2 | tab3
cat3 | tab1
cat4 | tab1
cat5 | tab1

I guess it's a one-to-many relationship tabs-to-cats. 我猜这是一对多的关系。 My question is how can I structure a query that will return only distinct tab names, each with all of its associated cats? 我的问题是我该如何构造一个查询,该查询将仅返回不同的选项卡名称,每个名称都包含所有与之相关的猫? I can get a result with all the cats and many duplicate tabs. 我可以得到所有猫和许多重复选项卡的结果。 Or I can get only distinct tabs but only one cat per tab. 或者我只能得到不同的标签,但每个标签只能有一只猫。 Maybe I should just be testing for duplicate tabs with my php code? 也许我应该只用我的PHP代码测试重复的标签? Or querying tabs then querying all the cats for each tab? 还是查询选项卡,然后查询每个选项卡的所有猫? It just seems like there has got to be a more direct way. 似乎必须要有一种更直接的方法。 I'm currently using this query: 我目前正在使用此查询:

SELECT t.name AS tab, c.name AS cat FROM tabs t
LEFT JOIN categories c
ON t.name=c.tab_name
ORDER BY t.order;

You can use MySQL's GROUP_CONCAT() to produce a comma-separated list of cat , if that is what you want: 您可以使用MySQL的GROUP_CONCAT()来生成cat的逗号分隔列表,如果需要的话:

SELECT
  t.name AS tab,
  GROUP_CONCAT(c.name) AS cats
FROM 
  tabs t 
  LEFT JOIN categories c ON t.name = c.tab_name
GROUP BY tab
ORDER BY t.`order`   /* ORDER is a reserved keyword and has to be quoted as `order` */

This would produce output like: 这将产生如下输出:

tab      cats
------------------------------
Tab1     Cat1,Cat2,Cat3,Cat4
Tab2     Cat4,Cat3,Cat6,Cat7

However, 然而,

the query you have which produces multiple rows per tab is a more conventional way to handle this. 您所查询的每个tab产生多行是一种更传统的处理方式。 In your application code, you would loop over the tab cat pairs and test for a change of the tab . 在您的应用程序代码中,您将遍历tab cat对,并测试tab的更改。 If you use the GROUP_CONCAT() above, you would also need to split it on the , in your application code. 如果您使用的GROUP_CONCAT()以上,你还需要将其分割上,在应用程序代码。

Either way, you cannot get a hierarchical result row-wise in the SQL - you have to repeat tab for each row. 无论哪种方式,您都无法在SQL中按行获取分层结果-您必须为每行重复tab That's just the nature of the 2-dimensional structure of a result set. 这只是结果集的二维结构的本质。 So it will require some application-side processing. 因此,它将需要一些应用程序端处理。

SQL ain't the best fit for hierarchical queries; SQL并不是最适合分层查询的方法; however, your query output lends itself well for processing inside the application: 但是,您的查询输出很适合在应用程序内部进行处理:

$tabs = array();
while ($row = $stmt->fetch()) {
    $tabs[$row['tab']][] = $row['cat'];
}

If you also need distinct categories: 如果您还需要不同的类别:

    $tabs[$row['tab']][$row['cat']] = $row['cat'];

It generates a two-dimensional array with tabs as the first dimension and categories as the second. 它生成一个二维数组,其中选项卡为第一维,类别为第二维。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM