I am a new programmer and I don't know how to do this.
I have:
- a table named
customers
(cust_id, name) - a table named
agents
(agent_id, agent_name) - a table named
authorizations
(cust_id, agent_id) - a table named
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
. So, I need to be able to pull only results for this particular cust_id
under this agent_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.c开发者_如何学运维ust_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,
Below is a query I used to connect 2 tables. It would be the same for your query, notice the syntax used starting at "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:
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.
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).
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.
Hope this sample helps you in future querying.
精彩评论