mysql从多表中选择问题

时间:2010-06-15 05:54:42

标签: mysql

这是查询

SELECT 
                        members.memberID,
                        members.salutation,
                        members.firstName,
                        members.middleName,
                        members.lastName,
                        members.suffix,
                        members.company,

                        addresses.address1,
                        addresses.address2,
                        addresses.city,
                        addresses.state,
                        addresses.postalCode,
                        addresses.country,
                        addresses.memberID,

                        email.email,
                        email.memberID,

                        phonenumbers.phoneNumber, 
                        phonenumbers.memberId,

                        subscriptions.year,
                        subscriptions.memberID
                    FROM 
                        members,
                        addresses,
                        email,
                        phonenumbers,
                        subscriptions
                    WHERE
                        subscriptions.year = '%s'
                            AND
                        subscriptions.memberID = members.memberID
                            AND
                        subscriptions.memberID = addresses.memberID
                            AND
                        subscriptions.memberID = email.memberID
                            AND
                        subscriptions.memberID = phonenumbers.memberID
                    ORDER BY 
                        members.lastName,
                        members.firstName,
                        members.company
LIMIT 0, 10

我的问题是它是一个巨大的查询所以我试图将它限制为一次这么多...它应该有超过5000个结果...无论如何,唯一有效的限制是限制0,10,如果你做任何事情否则5,10它不起作用0,50不起作用......只有0,10个工作......

当我做0,10时,查询返回blake firstName,middleName,lastName和其他几个......当我在$ result上执行print_r()时,它也显示为空白,并且有大多数def数据库中的数据,也没有错字...

2 个答案:

答案 0 :(得分:1)

我想建议在用于链接两个表并应用于条件的字段中使用左外连接和索引。它应该增加性能...

因为我获得了主表中50,000行的记录和多行子表的表现......

尝试正确地离开外连接并进行正确的索引

编辑:使用SQL语句:

SELECT 
    subscriptions.year,
    subscriptions.memberID

    members.memberID,
    members.salutation,
    members.firstName,
    members.middleName,
    members.lastName,
    members.suffix,
    members.company,

    addresses.address1,
    addresses.address2,
    addresses.city,
    addresses.state,
    addresses.postalCode,
    addresses.country,
    addresses.memberID,

    email.email,
    email.memberID,

    phonenumbers.phoneNumber, 
    phonenumbers.memberId,

    FROM 
    subscriptions
    left outer join members on subscriptions.memberID = members.memberID
    left outer join addresses on subscriptions.memberID = addresses.memberID
    left outer join email on subscriptions.memberID = email.memberID
    left outer join phonenumbers on subscriptions.memberID = phonenumbers.memberID
    WHERE
    subscriptions.year = '%s'
    ORDER BY 
    members.lastName,
    members.firstName,
    members.company
LIMIT 0, 10

答案 1 :(得分:1)

根据您的会员平均有多少电子邮件地址和电话号码,您的查询可能存在问题。假设您有记录(为简洁起见省略了大多数字段):

members                             addresses             phoneNumbers
memberId | firstName | lastName     memberId | city       memberId | phoneNumber
---------------------------------   -------------------   ----------------------
839      | Joe       | Hallenbeck   839      | New York   839      | 111-11111
                                    839      | L.A.       839      | 222-22222

那么你的加入将会为Joe Hallenbeck创造一条记录,但只有四条:

memberId | firstName | lastName    | city     | phoneNumber
-----------------------------------------------------------
839      | Joe       | Hallenbeck  | New York | 111-1111
839      | Joe       | Hallenbeck  | New York | 222-2222
839      | Joe       | Hallenbeck  | L.A.     | 111-1111
839      | Joe       | Hallenbeck  | L.A.     | 222-2222

我不知道这是否是你想要的,但你应该知道这个查询可能会产生比数据库中的成员多得多的结果记录。

如果有更多的建议,你应该发布一些DDL语句(create table等),正如Konerak指出的那样。