I’m trying to build a query with 4 different tables:
- Users_Licences
- Licences
- Products
- Request
The Users have multiple Licences, stored in Users_Licences. Products have multiple Requests and in the Request table you have target licence where in the Products table you have source_licence.
I want to list all the requests that have target licences and source licences (via Products) that the specified user has in his Users_Licences table.
Complicated but this is where I’m at this point (not working):
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
Could you help me out? If I delete the line
JOIN Licences ON Products.source_licence_id = Licences.id
it works but I have part of my result…Thanks!
You need to use different alias name wheh you use the table more than a time