简体   繁体   English

mysql 嵌套选择语句

[英]mysql nested select statement

I am newbie for relational tables.我是关系表的新手。 As i learn more, i see many advantages of sql statement.随着我了解更多,我看到了 sql 语句的许多优点。 However i stuck on some point.但是我坚持了某一点。

i look for if can get a single table for desired data-set from 5 tables.我寻找是否可以从 5 个表中获取所需数据集的单个表。 One of the table named 'eta' is used to connect other 4 tables.其中一个名为“eta”的表用于连接其他 4 个表。

Aim: A phone number or email can be used by many users, and users may also have multiple phone number or email address.目的:一个电话号码或邮箱可以被多个用户使用,用户也可能有多个电话号码或邮箱。 I want to list the phone numbers or email addresses with the user name that uses them.我想列出电话号码或电子邮件地址以及使用它们的用户名。

Sample Data: Please check http://sqlfiddle.com/#!2/60acce for the table structure and sample data-set示例数据:请查看http://sqlfiddle.com/#!2/60acce以获取表结构和示例数据集

The final expected final result for the phone case as follow:手机壳的最终预期结果如下:

  phone            name    extra
  45336467      ABC Co.   
  45336467      Gery      114
  45336467      Todd      117
  45336467      Adam      119

Thanks谢谢

Edit: Explanation for the table relation.编辑:表关系的解释。

table 'eta' has 4 fields which have table relations.表 'eta' 有 4 个具有表关系的字段。 column 'own' shows the company, person etc value 'f' for company and 'k' for person others will used for different tables 'own' 列显示公司、人员等值 'f' 代表公司,'k' 代表其他人将用于不同的表格

v1_id for id of own field's referred table.. v1_id 用于自己字段的引用表的 id..

column 'res' refers to email and phone tables. “res”列指的是电子邮件和电话表。 value 't' for phone and 'e' for email.电话的值 't' 和电子邮件的值 'e'。 v2_id for id of res field's referred table v2_id 用于 res 字段的引用表的 id

After filing through your schema, I believe the following query should work for you:通过您的架构归档后,我相信以下查询应该适合您:

SELECT
    phone.tel AS phone,
    CASE WHEN person.name IS NULL THEN company.titles ELSE person.name END AS name,
    eta.extra AS extra
FROM
    eta
    LEFT JOIN person
        ON eta.v1_id = person.id
        AND eta.own = 'k'
    LEFT JOIN company
        ON eta.v1_id = company.id
        AND eta.own = 'f'
    JOIN phone
        ON phone.id = eta.v2_id
WHERE
    eta.res = 't'

This assumes that eta.res = 't' if the relationship is to the "phone" table.如果关系是“电话”表,则假定eta.res = 't' Also, eta.own = 'k' for people, eta.own = 'f' for companies.此外, eta.own = 'k' eta.own = 'f'eta.own = 'f'代表公司。

It uses two left joins to pull from the person / company tables so that it can accomplish the pulls in a single query.它使用两个左连接从person / company表中提取,以便它可以在单个查询中完成提取。 If the person.name column is null, that means the current record is from the company table and will select that value instead (for the name column).如果person.name空,则表示当前记录来自company表并将选择该值(对于 name 列)。

The same can be done to select the email addresses (assuming eta.res = 'e' to relate to the email table):同样可以选择电子邮件地址(假设eta.res = 'e'与电子邮件表相关):

SELECT
    email.email AS email,
    CASE WHEN person.name IS NULL THEN company.titles ELSE person.name END AS name
FROM
    eta
    LEFT JOIN person
        ON eta.v1_id = person.id
        AND eta.own = 'k'
    LEFT JOIN company
        ON eta.v1_id = company.id
        AND eta.own = 'f'
    JOIN email
        ON email.id = eta.v2_id
WHERE
    eta.res = 'e'

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

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