在内连接上获取多于一列
我有以下查询:
SELECT c.company_id, c.company_title, c.featured, a.address, a.postal_code, pc.city, pc.region,cat.category_title, p.phone
FROM dloc.companies c
INNER JOIN dloc.address a ON ( c.address_id = a.address_id )
INNER JOIN dloc.postal_code pc ON ( a.postal_code = pc.postal_code )
INNER JOIN dloc.company_categories cc ON ( c.company_id = cc.company_id )
INNER JOIN dloc.categories cat ON ( cc.category_id = cat.category_id )
INNER JOIN dloc.phones p ON ( c.company_id = p.company_id )
WHERE c.company_title like '%gge%'
一切正常。 唯一的事情是..好吧。 phones
包含某些公司的多个电话号码... 我想我只得到第一个......或者随机的,我不确定。 我如何重写此查询,以便它返回每个公司的所有电话号码?
i have the following query:
SELECT c.company_id, c.company_title, c.featured, a.address, a.postal_code, pc.city, pc.region,cat.category_title, p.phone
FROM dloc.companies c
INNER JOIN dloc.address a ON ( c.address_id = a.address_id )
INNER JOIN dloc.postal_code pc ON ( a.postal_code = pc.postal_code )
INNER JOIN dloc.company_categories cc ON ( c.company_id = cc.company_id )
INNER JOIN dloc.categories cat ON ( cc.category_id = cat.category_id )
INNER JOIN dloc.phones p ON ( c.company_id = p.company_id )
WHERE c.company_title like '%gge%'
everything works just fine.
the only thing is.. well. phones
contains more then one phone number for some companies...
and i guess i get only the first one... or random, im not sure.
how can i rewrite this query so it will return all the phone numbers for each company?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据该查询,您应该为每个电话号码行(每个公司)获取一行。 SQL 作为关系/集合数学/理论工作 - 具有“多”关系意味着返回多行(因此,您的查询应该已经执行所需的行为)。
通常,编写查询的人遇到的问题不是获取多行,而是将其限制为所需的“单”行。
编辑:
结果排序 -
按照惯例,SQL 返回无序的内容,除非给出了某种显式排序(通过使用
ORDER BY
子句)。您会看到“首先”返回“随机”电话号码,因为 RDBMS 仍然必须按顺序读取/返回结果;这个顺序是在运行时确定的(......通常),当系统选择在访问数据时使用哪些索引时。完整的交互相当复杂(并且可能是特定于供应商的),因此请记住这一点:除非您指定结果的排序,否则结果将以随机顺序返回
时期。
Given that query, you should get one row for each phone number row (per company). SQL works as relationship/set math/theory - having a '-many' relationship means multiple rows returned (So, your query should already be perfroming the desired behaviour).
Often, the problem people writing queries experience isn't getting multiple rows - it's restricting it to the desired 'single' row.
EDIT:
Result Ordering -
SQL, by convention, returns things unordered, unless some sort of explicit ordering is given (through the use of an
ORDER BY
clause). You are seeing 'random' phone numbers being returned 'first' because the RDBMS still has to read/return results sequentially; this order is determined at runtime (...usually), when the system picks what indicies to use while accessing data. The full interaction is rather complex (and is probably vendor specific), so just keep this in mind:UNLESS YOU SPECIFY ORDERING OF YOUR RESULTS, THE RESULTS ARE RETURNED IN A RANDOM ORDER
period.
我建议您使用
GROUP_CONCAT
在p.phone
字段上。不过,有一些考虑因素:
INNER JOIN
更改为LEFT JOIN
以考虑到这一点。I would suggest you using
GROUP_CONCAT
on thep.phone
field.A few considerations, though:
INNER JOIN
toLEFT JOIN
to take that into consideration.您应该获取给定公司 ID 的所有电话号码,因为联接首先在两个表之间执行笛卡尔积,然后删除与联接中的条件不匹配的所有“行”。
You should get all phone numbers for the given company id because the join begins by doing a cartesian product between the two tables and then removing all "rows" that doesn't match the criteria in the join.
group_concat
会将所有电话号码收集在一列中。如果您确定每个公司没有多次列出其他值,请在 company_id 上添加
分组依据
。如果没有,请将这些“重复”(因为需要更好的词)值也放入
group_concat
中。请参阅: http://dev.mysql .com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
group_concat
will collect all phonenumbers in one column.Add a
group by
on company_id, if you are sure no other values are listed more than once per company.If not, put those 'duplicate' (for want of a better word) values in a
group_concat
as well.See: http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat