简体   繁体   English

MySQL嵌套查询如何选择多个行

[英]MySQL nested queries how to select more than one row

SELECT id, FIO, parent_id
FROM users
WHERE parent_id =
(
    SELECT id
    FROM users
    WHERE parent_id =
    (
        SELECT id
        FROM users
        WHERE id = 16
    )
)

So here I am making an hierarchy tree, first selecting the root parent, then the children's and so on to 24th level of depth. 因此,在这里我要制作一个层次树,首先选择根父级,然后选择子级,以此类推,直到第24层深度。

The question is: How to select more than one column from the inner queries? 问题是:如何从内部查询中选择多个列?

Because I need to get the other rows fields to display info like: name, surname, age 因为我需要获取其他行字段来显示信息,例如: name, surname, age

It looks like I can only get those columns of rows in the outer query (the topmost). 看来我只能在外部查询(最顶部)中获得那些行列。

PS: I don't want to use joins because they generate duplicate fields. PS:我不想使用联接,因为它们会生成重复的字段。

Is there a solution? 有解决方案吗?

You could iterate on the SQL side using MySQL query variables. 您可以使用MySQL查询变量在SQL端进行迭代。 This will return all childs with all data of one parent node without repeating yourself (and thus without imposing a limit on the depth of your tree) 这将返回具有一个父节点的所有数据的所有子节点,而不会重复您自己(因此不会对树的深度施加限制)

something like this: (500 being the parents id to start with) 像这样:(500是开始的父母身份)

SELECT
  id,
  parent_id, 
  name, 
  '0' as depth, 
  @tree_ids := id AS foo
FROM 
   tree,
  (SELECT @tree_ids := '', @depth := -1) vars
WHERE id = 500
UNION
SELECT 
  id,
  parent_id,
  name,
  @depth := IF(parent_id = 500, 1, @depth + 1) AS depth,
  @tree_ids := CONCAT(id, ',', @tree_ids) AS foo
FROM 
  tree 
WHERE FIND_IN_SET(parent_id, @tree_ids) OR parent_id = 500

See a working example at SQLfiddle 在SQLfiddle上查看工作示例

Note that this gives a really bad performance on larger datasets because MySQL will not use your indexes and instead will do a full table scan. 请注意, 这在大型数据集上的性能确实很差,因为MySQL不会使用您的索引,而是会进行全表扫描。 (i don't understand why its not using indexes, thats just how it is. if someone has advice on or explain the indexing issue, please comment!) (我不明白为什么它不使用索引,事实就是如此。如果有人对索引问题有建议或解释,请发表评论!)

= comparisons work on only a single value. =比较仅对单个值起作用。 You can use in to compare against multiple values: 您可以使用in与多个值进行比较:

SELECT ...
FROM yourtable
WHERE somefield IN (select somevalue from othertable);

Storing heirarchical data in mysql and getting it out is not as simple as that. 在mysql中存储分层数据并将其导出并不是那么简单。

Look into this: https://stackoverflow.com/a/4346009/9094 看看这个: https : //stackoverflow.com/a/4346009/9094

You will need more data to work with. 您将需要更多数据才能使用。

It seems your DB relationship is setup to be MPTT, here is a good blog post exaplaining how to query mysql MPTT data http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/ 看来您的数据库关系已设置为MPTT,这是一篇不错的博客文章,阐述了如何查询mysql MPTT数据http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/

Have a look at Full Tree example Retrieving a Full Tree in summary it can be done with joins. 看一看完整树示例,总结一下,检索完整树可以通过联接来完成。

I am not 100% sure if I understood exactly what you mean, but if you want to select all columns separately from the table in a subselect... 我不确定我是否完全理解您的意思,但是如果您想在子选择中从表中单独选择所有列,我不确定100%。

col1, col2, col3, col4

you would need for each column a single subselect that always matches against the same WHERE. 您将需要为每列提供始终与同一WHERE匹配的单个子选择。 Example: 例:

`SELECT * FROM main_table,
(SELECT col1 FROM inner_table WHERE inner_table.some_column=main_table.some_column),
(SELECT col2 FROM inner_table WHERE inner_table.some_column=main_table.some_column), ...`

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

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