一对多返回的数据过多 - MySQL

发布于 2024-09-03 06:12:22 字数 601 浏览 6 评论 0原文

我有 2 个相关的 MySQL 表,处于一对多关系。

客户: cust_id、cust_name、cust_notes

订单: order_id、cust_id、order_comments

因此,如果我执行标准联接以通过 PHP 获取所有客户及其订单,我会返回类似的内容:

  • 杰克·布莱克,杰克的笔记,关于杰克的一阶的评论
  • 杰克·布莱克,杰克的笔记,关于杰克的二阶的评论
  • 西蒙·史密斯,西蒙的笔记,关于西蒙的一阶的评论
  • 西蒙·史密斯,西蒙的笔记,关于西蒙的二阶的评论

问题是 < em>cust_notes 是一个文本字段,可能非常大(几千字)。因此,似乎为每个订单返回该字段是不够的。

我可以使用GROUP_CONCATJOINS返回单行上的所有order_comments,但order_comments也是一个大文本字段,所以看起来可以制造一个问题。

我是否应该只使用两个单独的查询,一个用于客户表,一个用于订单表?

有更好的办法吗?

I have 2 related MySQL tables in a one to many relationship.

Customers: cust_id, cust_name, cust_notes

Orders: order_id, cust_id, order_comments

So, if I do a standard join to get all customers and their orders via PHP, I return something like:

  • Jack Black, jack's notes, comments about jack's 1st order
  • Jack Black, jack's notes, comments about jack's 2nd order
  • Simon Smith, simon's notes, comments about simon's 1st order
  • Simon Smith, simon's notes, comments about simon's 2nd order

The problem is that cust_notes is a text field and can be quite large (a couple of thousand words). So, it seems like returning that field for every order is inneficient.

I could use GROUP_CONCAT and JOINS to return all order_comments on a single row BUT order_comments is a large text field too, so it seems like that could create a problem.

Should I just use two separate queries, one for the customers table and one for the orders table?

Is there a better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

别忘他 2024-09-10 06:12:22

看。你的页面很像 SO 上的问题页面。首先提出一个问题,然后在下面回答。
没有人费心通过一个查询来请求它!

答案 - 是的,使用 join,但仅获取名称和 ID,而不是完整的用户信息。
没什么复杂的。

只需对客户和订单列表使用单独的查询

Look. Your page is much like an question page on SO. A question first and answers below.
Noone bother to request it with one query!

Answers - yes, uses join, but to get names and id's only, not full user's info.
Nothing complicated.

Just use separate queries for the customer and for the list of orders

浅笑轻吟梦一曲 2024-09-10 06:12:22

如果我正确理解你的问题,最好使用两个查询,以获得如下结果:

Jack black
   comments
   comments
   comments
   comments

Simon Smith
   comments
   comments
   comments

If i understand your question correctly, it is better to use two query, for a results like:

Jack black
   comments
   comments
   comments
   comments

Simon Smith
   comments
   comments
   comments
迷迭香的记忆 2024-09-10 06:12:22

通常我们使用一些不同的策略。

在一种情况下,我们返回了多个结果集 - 一组用于父母,一组用于所有孩子。 ORDER BY 是相同的,因此您可以轻松地在客户端代码中为每个父级交错子级。

还可以返回多个结果集 - 一个用于父级,一组子级用于每个父级(我们在其外部使用游标)。

我们(在 SQL Server 中)使用的另一个东西是 XML,因此它实际上返回了分层形式。

Typically we used a few different strategies.

In one case we returned multiple result sets - one set for the parents and one for all the children. The ORDER BY was the same, so that you could easily interleave the children for each parent in the client code.

It was also possible to return multiple result sets - one for the parents and one set of children for EACH parent (we used a cursor for the outer part of this).

Another thing we used (in SQL Server) was XML so it actually returned the hierarchical form.

思慕 2024-09-10 06:12:22

您可以运行两个查询,一个用于注释,另一个用于客户详细信息。

// this will give you orders in an efficient way
SELECT cust_id, order_id, order_comments
FROM customers AS c
LEFT JOIN orders as o ON o.cust_id = c.cust_id
LIMIT xx, yy

// this will get all details from N specific customers
SELECT * FROM customers
WHERE cust_id = 'xxx' OR cust_id = 'yyy' OR cust_id = 'zzz'

然后将第二个查询传递给数组,并在显示第一个查询时使用存储在数组中的客户详细信息。

应该还是蛮有效率的。

You can run two queries, one for notes and one for the customer details.

// this will give you orders in an efficient way
SELECT cust_id, order_id, order_comments
FROM customers AS c
LEFT JOIN orders as o ON o.cust_id = c.cust_id
LIMIT xx, yy

// this will get all details from N specific customers
SELECT * FROM customers
WHERE cust_id = 'xxx' OR cust_id = 'yyy' OR cust_id = 'zzz'

And then pass the second query to an array and, as you're showing the first one, use the details from the customer that are stored in the array.

Should be pretty efficient.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文