简体   繁体   English

mySQL一对多查询

[英]mySQL one-to-many query

I've got 3 tables that are something like this (simplified here ofc): 我有3个像这样的表(在这里简化为ofc):

  • users 用户
    • user_id 用户身份
    • user_name 用户名
  • info 信息
    • info_id info_id
    • user_id 用户身份
    • rate
  • contacts 往来
    • contact_id CONTACT_ID
    • user_id 用户身份
    • contact_data contact_data

users has a one-to-one relationship with info, although info doesn't always have a related entry. 用户与信息具有一对一的关系,但信息并不总是具有相关条目。

users has a one-to-many relationship with contacts, although contacts doesn't always have related entries. 用户与联系人之间存在一对多关系,但联系人并不总是有相关条目。

I know I can grab the proper 'users' + 'info' with a left join, is there a way to get all the data I want at once? 我知道我可以通过左连接获取正确的'用户'+'信息',有没有办法立即获得我想要的所有数据?

For example, one returned record might be: 例如,一条返回的记录可能是:

user_id: 5
user_name: tom
info_id: 1
rate: 25.00
contact_id: 7
contact_data: 555-1212
contact_id: 8
contact_data: 555-1315
contact_id: 9
contact_data: 555-5511

Is this possible with a single query? 单个查询可以实现吗? Or must I use multiple? 或者我必须使用多个?

It is possible to do what you're asking in one query, but you'd either need a variable number of columns which is evil because SQL isn't designed for that, or you'd have to have a fixed number of columns, which is even more evil because there is no sensible fixed number of columns you could choose. 可以在一个查询中执行您要求的操作,但是您需要一个可变数量的列,这是邪恶的,因为SQL不是为此设计的,或者您必须有固定数量的列,这更加邪恶,因为你可以选择没有合理固定数量的列。

I'd suggest using one of two alternatives: 我建议使用以下两种方法之一:

1. Return one row for each contact data, repeating the data in other columns: 1.为每个联系人数据返回一行,重复其他列中的数据:

5 tom 1 25.00 7 555-1212
5 tom 1 25.00 8 555-1315
5 tom 1 25.00 9 555-5511

The problem with this of course is that redundant data is normally a bad idea, but if you don't have too much redundant data it will be OK. 当然这个问题是冗余数据通常是一个坏主意,但如果你没有太多冗余数据就可以了。 Use your judgement here. 在这里使用你的判断。

2. Use two queries. 2.使用两个查询。 This means a slightly longer turnaround time, but less data to transfer. 这意味着周转时间稍长,但传输的数据较少。

In most cases I'd prefer the second solution. 在大多数情况下,我更喜欢第二种解决方案。

You should try to avoid making a large number of queries inside a loop. 您应该尽量避免在循环中进行大量查询。 This can almost always be rewritten to a single query. 这几乎总是可以重写为单个查询。 But if using two queries is the most natural way to solve your problem, just use two queries. 但是,如果使用两个查询是解决问题的最自然的方法,那么只需使用两个查询。 Don't try to cram all the data you need into a single query just for the sake of reducing the number of queries. 不要为了减少查询次数而尝试将所需的所有数据塞入单个查询中。

Each row of result must have the same columns, so you can't aggregate multiple rows of contact not having the other columns as well. 每行结果必须具有相同的列,因此您不能聚合多行不具有其他列的联系人。

Hopefully, this query would achieve what you need: 希望这个查询可以满足您的需求:

SELECT
    u.user_id as user_id,
    u.user_name as user_name,
    i.info_id as info_id,
    i.rate as rate,
    c.contact_id as contact_id,
    c.contact_data as contact_data
FROM users as u
LEFT JOIN info as i ON i.user_id = u.user_id
LEFT JOIN contacts as c ON c.user_id = u.user_id

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

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