doufu5747 2015-01-22 09:30
浏览 13
已采纳

MySQL非常简单的连接示例请求子表

I keep falling back into questions with MySQL joining. And I would like to request a very simple example I could use to continue my journey of understanding learning the MySQL syntax.

Let's say I got the following table's

test_testtable

  • testtable_id
  • testtable_name
  • testtable_user
  • testtable_option
  • testtable_textfield

test_testlink

  • testlink_id
  • testlink_link
  • testlink_address

test_address

  • address_id
  • address_name
  • address_phone
  • address_email
  • address_street
  • address_city
  • address_zip

I would like to make a selection like :

SELECT * (lets say I would define the fields) FROM `test_testable` 
JOIN `test_testtable`.`testtable_id` = `test_testlink`.`testlink_link`
AND
JOIN `test_testlink`.`testlink_addres` = `test_address`.`address_id`
WHERE `user_id` = 5

Hence the linking structure is like:

test_testtable.testtable_id = leading

table test_testlink is a table to link the table test_testtable and test_address

And linking table test_testlink uses the field testlink_link to link to the table test_testtable, and uses the field testlink_address to link to the table test_address

This does not work. FOR ME.. Since I continuously seem to fail of catching the correct syntax logic.

So I hope that someone could give me a small example of how to correctly implement such a simple yet critical query!

TIAD!!

  • 写回答

3条回答 默认 最新

  • douqiang5163 2015-01-22 09:40
    关注

    A general approach :

    SELECT table1.* FROM table1
    JOIN table2 ON table2.id_table1 = table1.id
    JOIN table3 ON table3.id_table2 = table2.id
    WHERE table1.id = 10
    

    For your purpose :

    SELECT * (lets say I would define the fields) FROM `test_testable` 
    JOIN `test_testlink` ON `test_testtable`.`testtable_id` = `test_testlink`.`testlink_link`
    JOIN `test_address` ON `test_testlink`.`testlink_addres` = `test_address`.`address_id`
    WHERE `user_id` = 5
    

    Please read the reference

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题