0

我有一个“用户”表、一个“报价”表和一个带有用户 ID 和报价 ID 的联结“*users_offers*”。

如何从属于特定用户的报价中选择每个报价?

我可以实现一个解决方案(如下),它实际上从联结表中开始选择数据,然后简单地连接报价列,但结果也包含联结列。

SELECT * 
FROM users_offers INNER JOIN offers ON users_offers.offerID = offers.ID 
WHERE userID = 1

有没有从优惠开始选择的解决方案?

4

2 回答 2

2

使用SQL 别名

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 语句的其他部分。

于 2013-04-20T22:48:51.320 回答
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
于 2013-04-20T22:54:48.080 回答