【发布时间】:2011-06-07 21:48:36
【问题描述】:
我是一个新程序员,我不知道该怎么做。
我有:
- 名为
customers(cust_id,名称)的表 - 一个名为
agents(agent_id,agent_name)的表 - 名为
authorizations(cust_id,agent_id)的表 - 名为
product_services(p_s_id、cust_id、agent_id、item_name、item_id)的表
关于authorizations 表:agent_id 是cust_id 的授权销售人员的代理。因此,我需要能够在此agent_id 下仅提取此特定cust_id 的结果。
我需要进行查询,以返回客户在所有其他代理下拥有的所有产品和服务。在搜索中,它还应该返回他们在我手下拥有的产品/服务。
这是我迄今为止尝试过的:
$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";
【问题讨论】:
-
对于 db 相关的问题,最好提一下您使用的数据库。
-
从语法上看,我觉得是mysql
-
对不起!我正在使用 phpmyadmin。
-
yes123 的意思是,'phpmyadmin' 是一个管理界面,而不是一个数据库。我猜你当时正在使用 MySQL :)
标签: php mysql sql join relationship