mysql 1066_帮助MySQL查询语法:错误#1066 - 不唯一的表/别名

bd96500e110b49cbb3cd949968f18be7.png

I have four tables, user, user_billingprofile, user_shippingprofile, and user_address.

user: userId, dateCreated

user_billingprofile: userId, address

user_shippingprofile: userId, address

user_address: random address crap

Here is the query I have to get a users billing and shipping profiles in one shot.

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`

I get the error: #1066 - Not unique table/alias: 'user_address'.

Is there a way to take a simple join where a table is accessed twice in the same query, and separate the two results? Preferably with some kind of table prefix...

I'm a bit lost here. I know I could do this in two sepparate queries quite easily, but i'd like to learn how to do stuff like this in one shot.

Any help/suggestions/direction is greatly appreciated, thank you!.

解决方案

Can you post the structure of your tables? Based on your query I'd say you need to consider changing it up a bit.

That said you can fix your current query by adding a table alias like so:

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`

Note the AS clause I added. You'll probably need to alias the columns too (instead of SELECT * you likely will need SELECT user_shipping_address.address AS user_shipping_address_value, user_billing_address.address AS user_billing_address_value ... )

Hope that helps!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值