我有一个'用户'表,一个'商品'表,以及一个带有userID和offerID的联结'* users_offers *'。
如何从属于特定用户的优惠中选择每个优惠?
我可以实现一个实际上从联结表开始选择数据的解决方案(下面),然后简单地加入商品列,但结果也包含结点列。
SELECT *
FROM users_offers INNER JOIN offers ON users_offers.offerID = offers.ID
WHERE userID = 1
是否有任何解决方案可以开始选择优惠?
答案 0 :(得分:2)
使用SQL Alias:
SELECT o.*
FROM users_offers AS uo
INNER JOIN offers AS o ON uo.offerID = o.ID
WHERE uo.userID = 1
说明:
FROM users_offers AS uo
设置一个名为“uo”的新别名,指向[users_offers]表。 offers AS o
也是如此。这些别名可以在SQL语句的其他部分中使用。
答案 1 :(得分:0)
SELECT offers_alias.*
FROM users_offers
INNER JOIN offers AS offers_alias ON users_offers.offerID = offers_alias.ID
WHERE users_offers.userID = 1