[英]How to join all tables?
I am a new programmer and I don't know how to do this.我是一个新程序员,我不知道该怎么做。
I have:我有:
customers
(cust_id, name)一个名为customers
(cust_id, name) 的表agents
(agent_id, agent_name)名为agents
的表(agent_id,agent_name)authorizations
(cust_id, agent_id)一个名为authorizations
(cust_id,agent_id)的表product_services
(p_s_id, cust_id, agent_id, item_name, item_id)名为product_services
的表(p_s_id、cust_id、agent_id、item_name、item_id) About the authorizations
table: the agent_id
is the agent who the is authorised salesperson for cust_id
.关于authorizations
表: agent_id
是cust_id
的授权销售人员的代理。 So, I need to be able to pull only results for this particular cust_id
under this agent_id
.因此,我需要能够仅提取此agent_id
下此特定cust_id
的结果。
I need to do a query that will return all the products and services that the customer have under all other agents.我需要做一个查询,返回客户在所有其他代理下拥有的所有产品和服务。 In the search, it should also return products/services that they have under me as well.在搜索中,它还应该返回他们在我手下拥有的产品/服务。
This is what I have tried so far:这是我到目前为止所尝试的:
$sql = "SELECT
product_services.item_name, agents.agent_name,
customers.name, agents.agent_id, product_services.item_id
FROM
product_services
LEFT JOIN agents ON product_services.agent_id = agents.agent_id
LEFT JOIN customers ON product_services.cust_id = customers.cust_id
WHERE authorizations.agent_id = '$aid'
AND product_services.item_name LIKE '%$q%'
ORDER BY product_services.id DESC
LIMIT $start, $limit";
KPO, KPO,
Below is a query I used to connect 2 tables.下面是我用来连接 2 个表的查询。 It would be the same for your query, notice the syntax used starting at "LEFT JOIN"您的查询将是相同的,请注意从“LEFT JOIN”开始使用的语法
SELECT
user.username,
groups.group_id, groups.group_name,
sign.last_connected, sign.sign_id, sign.sign_name, sign.resolution_x, sign.resolution_y, LEFT(sign.sign_name, 1) AS first_char
FROM
user
LEFT JOIN(
groups, sign
)ON(
user.user_id = groups.userID AND
groups.group_id = sign.groupID
)
WHERE
username = ? AND
UPPER(sign.sign_name) BETWEEN "A" AND "Z"
OR sign.sign_name BETWEEN "0" AND "9" ORDER BY sign.sign_name
Considering your SQL query is initially correct this should work for your LEFT JOIN portion:考虑到您的 SQL 查询最初是正确的,这应该适用于您的 LEFT JOIN 部分:
LEFT JOIN(agents, customers
)ON( product_services.agent_id = agents.agent_id
AND product_services.cust_id = customers.cust_id)
Typically, a query would start with the core table at the root of your criteria... Such as a single customer and join from that... You also state you want ALL products/services from ALL agents including the "me" agent, you would not want to have a filter on your '$aid' criteria.通常,查询将从您的标准根目录处的核心表开始......例如单个客户并从中加入......您不希望对您的“$aid”标准进行过滤。
SELECT STRAIGHT_JOIN
c.cust_id,
c.name,
ag.agent_id,
ag.agent_name,
ps.item_name,
ps.p_s_id,
ps.item_id
from
customers c
join authorizations au
on c.cust_id = au.cust_id
[[ AND au.agent_id = '$aid' ]]
join agents ag
on au.agent_id = ag.agent_id
join product_services ps
on c.cust_id = ps.cust_id
AND au.agent_id = ps.agent_id
[[ AND ps.item_name like '%$q%' ]]
order by
ps.p_s_id DESC
limit
$start, $limit
In the query above, I've put in section where you could apply your在上面的查询中,我放置了您可以应用的部分
[[ AND agent criteria ]]
or product/service criteria.或产品/服务标准。 But as you stated, you wanted ALL activity of ALL agents, so I've left it out... Likewise with a possible但正如你所说,你想要所有代理的所有活动,所以我把它排除在外......同样有一个可能
[[ AND product / service ]]
criteria which may/may not be provided and you can very simply strip it out...可能/可能不提供的标准,您可以非常简单地将其删除...
-- EDIT PER COMMENT FEEDBACK. - 编辑每个评论反馈。
As per your inquiry on how to add more "criteria", its based on the origin of the table.根据您对如何添加更多“标准”的询问,它基于表格的来源。 If its the FIRST table in the "FROM" clause, you'll add a WHERE clause and put criteria to that table... As for the others, like the agents and product services, where I had the [[ criteria ]], you could just expand your entire criteria there (per respective table its joined with).如果它是“FROM”子句中的第一个表,您将添加一个 WHERE 子句并将条件放入该表......至于其他,如代理和产品服务,我有 [[ 条件 ]],您可以在那里扩展您的整个标准(每个表格与其连接)。
Such as your [[ product service criteria ]], I could have added something like比如你的[[产品服务标准]],我可以添加类似的东西
AND ( ( ps.Item like '%$something'
OR ps.Item like '%$another'
OR ps.Item like '%$more' )
AND ps.OtherField = whatever )
Keep your primary "join" conditions which identify the relationship between the tables first and foremost... only THEN do you want to add your restricting criteria... As you can see in my sample above, I've wrapped the entire AND ( ) clause within parenthesis to see it as a single "unit" condition... such as to the product/service table.保持你的主要“加入”条件,它首先确定表之间的关系......只有然后你想添加你的限制条件......正如你在上面的示例中看到的那样,我已经包装了整个 AND ( ) 括号中的子句将其视为单个“单元”条件……例如产品/服务表。
Hope this sample helps you in future querying.希望此示例对您将来的查询有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.