简体   繁体   English

试图找到如何将三个表链接在一起

[英]Trying to find how to link three tables together

At the moment I have three tables that I am trying to connect and figure out what queries will get the results I need and also follow best practices. 目前,我正在尝试连接三个表,并弄清楚哪些查询将获得所需的结果并遵循最佳实践。 MySQL is still pretty new to me, and this is my first stumbling block that I can't figure out. MySQL对我来说仍然很新,这是我不知道的第一个绊脚石。

I am trying to build a simple URL shortener that can "link" multiple long URLs to one short URL. 我正在尝试构建一个简单的URL缩短器,它可以将多个长URL“链接”到一个短URL。 I basically want to have a link www.example.com/google then have google.com, google.co.uk, google.it, etc... and GEO target when the user accesses the link. 我基本上想拥有一个链接www.example.com/google,然后在用户访问该链接时具有google.com,google.co.uk,google.it等...以及GEO目标。

My three tables are set up as: 我的三个表设置为:

short_id | short_id | user_id | user_id | short_url //Short URL Table short_url //短网址表

long_id | long_id | user_id | user_id | long_url | long_url | country_code //Long URL table country_code //长网址表

user_id | user_id | name | 名称| password | 密码 email | 电邮| created //User table 创建//用户表

I am not sure if foreign keys are the best route. 我不确定外键是否是最佳途径。 Also, I understand how to add a user, but what queries would I have to run to have a user add a short/long url and have the user_id field in "user" table match the user_id fields in the other tables. 另外,我了解如何添加用户,但是要使用户添加短/长url并使“用户”表中的user_id字段与其他表中的user_id字段匹配,我必须运行哪些查询。

Thanks for the help. 谢谢您的帮助。

select * from short_url_table
left join long_url_table
using (user_id) 
left join user_table 
using (user_id) ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM