我有四个表,user
,user_billingprofile
,user_shippingprofile
和user_address
。
用户: userId,dateCreated
user_billingprofile: userId,地址
user_shippingprofile: userId,地址
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'
。
有没有办法在同一个查询中进行两次表访问的简单连接,并将两个结果分开?最好使用某种表前缀......
非常感谢任何帮助/建议/方向,谢谢!
答案 0 :(得分: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 ...
)
希望有所帮助!