1

我有四个表,useruser_billingprofileuser_shippingprofileuser_address

user: userId, dateCreated
user_billingprofile: userId, address
user_shippingprofile: userId, address
user_address:随机地址废话

这是我必须一次性获取用户计费和运输配置文件的查询。

SELECT * FROM `user`
  JOIN `user_billingprofile`  ON `user`.`userId` = `user_billingprofile`.`userId`
    JOIN `user_address` ON `user_billingprofile`.`currentAddress` = `user_address`.`addressId`
  JOIN `user_shippingprofile` ON `user_shippingprofile`.`currentAddress` = `user_address`.`addressId`
    JOIN `user_address` ON `user_shippingprofile`.`currentAddress` = `user_address`.`addressId`

我得到错误:#1066 - Not unique table/alias: 'user_address'有没有办法进行一个简单的连接,在同一个查询中访问一个表两次,并将两个结果分开?最好带有某种表前缀...

我在这里有点迷路了。我知道我可以很容易地在两个单独的查询中做到这一点,但我想学习如何一次性完成这样的事情。

非常感谢任何帮助/建议/方向,谢谢!

4

1 回答 1

3

你能张贴你的桌子的结构吗?根据您的查询,我会说您需要考虑对其进行一些更改。

也就是说,您可以通过添加表别名来修复当前查询,如下所示:

SELECT * FROM `user`
  JOIN `user_billingprofile`  ON `user`.`userId` = `user_billingprofile`.`userId`
    JOIN `user_address` AS user_billing_address ON `user_billingprofile`.`currentAddress` = `user_address`.`addressId`
  JOIN `user_shippingprofile` ON `user_shippingprofile`.`currentAddress` = `user_address`.`addressId`
    JOIN `user_address` AS user_shipping_address ON `user_shippingprofile`.`currentAddress` = `user_address`.`addressId`

请注意AS我添加的条款。您可能还需要为列加上别名(而不是SELECT *您可能需要SELECT user_shipping_address.address AS user_shipping_address_value, user_billing_address.address AS user_billing_address_value ...

希望有帮助!

于 2010-10-14T18:32:47.157 回答