0

我正在尝试使用 4 个不同的表构建查询:

  • 用户_许可证
  • 许可证
  • 产品
  • 要求

Users多个Licences,存储在Users_Licences. Products有多个Requests,并且在Request表中你有目标许可证,在Products表中你有source_licence

我想列出Products指定用户在其Users_Licences表中拥有的所有具有目标许可证和源许可证(通过)的请求。

很复杂,但这是我现在所处的位置(不工作):

SELECT 
    Request.id AS offer_id, Request.product_id, 
    Request.licence_id AS target_licence_id, 
    Request.trans_price, Products.source_licence_id    
FROM 
    Request    
JOIN 
    Products ON Request.product_id = Products.id    
JOIN 
    Licences ON Products.source_licence_id = Licences.id    
JOIN 
    Licences ON Request.licence_id = Licences.id    
JOIN 
    Users_Licences ON Licences.id = Users_Licences.licence_id    
WHERE 
    Users_Licences.user_id = '$user_id'    
GROUP BY 
    Request.id    
ORDER BY 
    Request.trans_price DESC

你能帮帮我吗?如果我删除该行

JOIN Licences ON Products.source_licence_id = Licences.id

它有效,但我有部分结果......谢谢!

4

1 回答 1

1

当您多次使用该表时,您需要使用不同的别名

SELECT Request.id AS offer_id, Request.product_id, Request.licence_id AS target_licence_id, Request.trans_price 
, Products.source_licence_id     
FROM Request     
JOIN Products ON Request.product_id = Products.id     
JOIN Licences as l1 ON Products.source_licence_id = l1.id     
JOIN Licences as l2 ON Request.licence_id = l2.id     
JOIN Users_Licences ON l2.id = Users_Licences.licence_id     
WHERE Users_Licences.user_id='$user_id'     
GROUP BY Request.id     
ORDER BY Request.trans_price DESC 
于 2012-07-20T13:43:15.900 回答